简体   繁体   English

为充气菜单添加自定义图标

[英]Add custom icon for inflated menu

I am trying to add icon to the menu (since it looks so boring) 我想在菜单中添加图标(因为它看起来很无聊)

在此输入图像描述

My menu xml: 我的菜单xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_about"
        android:orderInCategory="200"
        android:showAsAction="never"
        android:icon="@drawable/icon"
        android:title="About" />
    <item
        android:id="@+id/action_help"
        android:orderInCategory="200"
        android:showAsAction="never"
        android:icon="@drawable/help"
        android:title="Help" />
    <item
        android:id="@+id/action_rate"
        android:orderInCategory="300"
        android:showAsAction="never"
        android:title="Rate This App"
        android:icon="@drawable/star" />
    <item
        android:id="@+id/action_quit"
        android:orderInCategory="400"
        android:showAsAction="never"
        android:title="Quit"
        android:icon="@drawable/quit" />

</menu>

Part of the Java code: 部分Java代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    //Set icon for the menu button
    Drawable icon = getResources().getDrawable(R.drawable.icon);
    menu.getItem(0).setIcon(icon);

    return true;
}

I still don't see the icon on the menu. 我仍然没有看到菜单上的图标。 I am running my phone and the AVD 4.2+. 我正在运行手机和AVD 4.2+。 Is it that Google took out icon support for menu? Google是否取消了菜单的图标支持?

Hopefully this is a dumb question, but do you have the icon image in one of your drawable folders with the name you are using to access it? 希望这是一个愚蠢的问题,但你有一个可绘制的文件夹中的图标图像,你用它来访问它吗?

If everything is set up correctly, try cleaning your project & running it again (making sure to build it as well). 如果一切设置正确,请尝试清理项目并再次运行(确保也可以构建它)。

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

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