How to show icons in ActionBar overflow menu?

冷暖自知 提交于 2020-01-31 04:10:30

问题


I want to display icon in addition to menu item title in overflow dropdown menu.

Is is possible?


回答1:


put your menu with property android:showAsAction="always" and in xml file of menu add sub menus under that your menu like this

<item
        android:id="@+id/mainMenu"
        android:icon="@drawable/launcher"
        android:showAsAction="always">
        <menu>

            <item
                android:id="@+id/menu_logout"
                android:icon="@drawable/log_out"
                android:title="logout"/>
        </menu>
    </item>

this will show the icons in menus




回答2:


There is actually an easy way to achieve it, since what you see is actually a TextView, so you set a span , for example:

final MenuItem menuItem=...
final ImageSpan imageSpan=new ImageSpan(this,R.drawable.ic_stat_app_icon);
final CharSequence title=" "+menuItem.getTitle();
final SpannableString spannableString=new SpannableString(title);
spannableString.setSpan(imageSpan,0,1,0);
menuItem.setTitle(spannableString);

This will put an icon at the beginning of the menu item, right before its original text.




回答3:


Maybe you have the same problem as me. So for me solution was easy if you are using AppCompat just dont use this property:

android:showAsAction="always"

instead use it like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/option"
        android:title="@string/option"
        android:icon="@drawable/option"
        app:showAsAction="always">
    </item>
</menu> 

There is difference in aditional xmlns:app and showAsAction is property of app.

Hope it helps someone.




回答4:


Hate answers like this but I am not good enough to give a better answer.

The icon for the overflow items for menu is the submenu2 line "subMenu2Item.setIcon(R.drawable.ic_launcher);" just replace ic_launcher with your icon file (in the res drawable directory of your program). Hope you mean using ActionBarSherlock.

The line subMenu1Item.setIcon(R.drawable.ic_launcher); is for the ActionBar icon for the Menu button at the top.

in response to "This is not possible by design"

Many thanks to Google for attempting to take over the world and letting us ride it's coat tails, BUT ... Google is too inconsistent to be grumpy over inconsistencies. It would be nice if Google could make an example for EVERY "set" of APIs they offer. There are API references to code, but no example about how to put them together, A whole completely working example that can be run as an APK is all I want. I can trick it out from there. If they can take the time to write the APK how much harder would it be to code a little example on a standard template? For 30% of everything it's not much to ask for. I had this same argument with Microsoft back in the day and it didn't happened then either. And thus and so you get stackoverflow. : ) Its a shame it takes some guy (rock on Jake Wharton) working on his own to do a better job with a backwards compatible Action bar than Google can come up with its ridiculous Action Bar Patch thingy.

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

    SubMenu subMenu1 = menu.addSubMenu("Action Item");
    subMenu1.add("Help");
    subMenu1.add("About");

    MenuItem subMenu1Item = subMenu1.getItem();
    subMenu1Item.setIcon(R.drawable.ic_launcher);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenu subMenu2 = menu.addSubMenu("All Options");
    subMenu2.add("Help");
    subMenu2.add("About");

    MenuItem subMenu2Item = subMenu2.getItem();
    subMenu2Item.setIcon(R.drawable.ic_launcher);

    return super.onCreateOptionsMenu(menu);
}



回答5:


@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod(
                    "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            }
            catch(NoSuchMethodException e){
                Log.e(TAG, "onMenuOpened", e);
            }
            catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Reference : How To show icons in Overflow menu in ActionBar




回答6:


This is not possible by design (at least in HC - and I'm developing on API 16 and getting no icons either). Google does not want us to do that.

For a bit of discussion on the topic, see related answer here:

  • Displaying icon for menu items of Action Bar in Honeycomb android 3.0

As one of the comments suggests, if you need the icons, consider using a dropdown menu, rather than the overflow menu.



来源:https://stackoverflow.com/questions/11982826/how-to-show-icons-in-actionbar-overflow-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!