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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11982826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:21:42  来源:igfitidea点击:

How to show icons in ActionBar overflow menu?

androidandroid-layoutactionbarsherlockandroid-ui

提问by Alexey Zakharov

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

除了溢出下拉菜单中的菜单项标题外,我还想显示图标。

Is is possible?

有可能吗?

回答by androido

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

这将显示菜单中的图标

回答by android developer

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,所以你设置了一个 span ,例如:

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.

这将在菜单项的开头放置一个图标,就在其原始文本之前。

回答by HugoMasterPL

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:appand showAsActionis property of app.

附加的xmlns:appshowAsActionapp 的属性有所不同。

Hope it helps someone.

希望它可以帮助某人。

回答by tester

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.

菜单溢出项的图标是 submenu2 行“subMenu2Item.setIcon(R.drawable.ic_launcher);” 只需将 ic_launcher 替换为您的图标文件(在程序的 res drawable 目录中)。希望你的意思是使用 ActionBarSherlock。

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

行 subMenu1Item.setIcon(R.drawable.ic_launcher); 用于顶部菜单按钮的 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. 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.

非常感谢谷歌试图接管世界并让我们骑在它的尾巴上,但是......谷歌太不一致了,不能因为不一致而脾气暴躁。如果谷歌能为他们提供的每一个“集合”API 做一个例子,那就太好了。有对代码的 API 参考,但没有关于如何将它们组合在一起的示例,我想要一个完整的可以作为 APK 运行的完整示例。我可以从那里骗过它。如果他们可以花时间编写 APK,那么在标准模板上编写一个小示例会更难吗?对于 30% 的一切,要求并不多。那天我和微软有过同样的争论,但当时也没有发生。因此,你得到了stackoverflow。:

@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);
}

回答by Abhinav

@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 的溢出菜单中显示图标

回答by Richard Le Mesurier

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.

这在设计上是不可能的(至少在 HC 中 - 我正在 API 16 上开发并且也没有图标)。Google 不希望我们这样做。

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.

正如评论之一所暗示的那样,如果您需要图标,请考虑使用下拉菜单,而不是溢出菜单。