简体   繁体   English

如何在ActionBar溢出菜单中显示图标?

[英]How to show icons in ActionBar overflow menu?

I want to display icon in addition to menu item title in overflow dropdown menu. 我想在溢出下拉菜单中显示菜单项标题以外的图标。

Is is possible? 有可能吗?

put your menu with property android:showAsAction="always" and in xml file of menu add sub menus under that your menu like this 把你的菜单与属性android:showAsAction="always"和菜单的xml文件中添加子菜单下你的菜单这样

<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 这将显示菜单中的图标

There is actually an easy way to achieve it, since what you see is actually a TextView, so you set a span , for example: 实际上有一种简单的方法可以实现它,因为你看到的实际上是一个TextView,所以你设置了一个跨度,例如:

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. 这将在菜单项的开头放置一个图标,就在其原始文本之前。

Maybe you have the same problem as me. 也许你和我有同样的问题。 So for me solution was easy if you are using AppCompat just dont use this property: 所以对我来说,如果您使用AppCompat只是不使用此属性,解决方案很容易:

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 . aditional xmlns有所不同:appshowAsActionapp的属性。

Hope it helps someone. 希望它可以帮助某人。

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);" 菜单溢出项的图标是子菜单行“subMenu2Item.setIcon(R.drawable.ic_launcher);” just replace ic_launcher with your icon file (in the res drawable directory of your program). 只需将ic_launcher替换为您的图标文件(在程序的res drawable目录中)。 Hope you mean using ActionBarSherlock. 希望你的意思是使用ActionBarSherlock。

The line subMenu1Item.setIcon(R.drawable.ic_launcher); 行subMenu1Item.setIcon(R.drawable.ic_launcher); is for the ActionBar icon for the Menu button at the top. 用于顶部“菜单”按钮的ActionBar图标。

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. 如果谷歌可以为他们提供的每一套“API”做一个例子,那就太好了。 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. 有代码的API引用,但没有关于如何将它们放在一起的示例,可以作为APK运行的完整工作示例是我想要的。 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? 如果他们可以花时间编写APK,那么在标准模板上编写一个小例子会有多难? For 30% of everything it's not much to ask for. 对于30%的一切,要求的并不多。 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. 因此,你得到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. :)令人遗憾的是,需要一些人(杰克沃顿的摇滚乐队)独立工作,用一个向后兼容的动作栏做得更好,而谷歌可以拿出它荒谬的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);
}
@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 参考: 如何在ActionBar的“溢出”菜单中显示图标

This is not possible by design (at least in HC - and I'm developing on API 16 and getting no icons either). 这在设计上是不可能的(至少在HC中 - 我正在开发API 16并且没有获得任何图标)。 Google does not want us to do that. 谷歌不希望我们这样做。

For a bit of discussion on the topic, see related answer here: 有关该主题的一些讨论,请参阅此处的相关答案:

As one of the comments suggests, if you need the icons, consider using a dropdown menu, rather than the overflow menu. 正如其中一条评论所暗示的,如果您需要图标,请考虑使用下拉菜单,而不是溢出菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM