Why the pictures/icon don't show up in the menu?

北城以北 提交于 2019-12-01 09:05:58

问题


I followed this tutorial to create a menu

but my menu looks differently:

How can I create a menu with images?

This is my code:

<menu xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:id="@+id/preferences"
        android:icon="@drawable/preferences"    
        android:title="Preferences" />
    <item android:id="@+id/help"
        android:title="Help"
        android:icon="@drawable/ic_action_search" />    
</menu>

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        /*menu.add(Menu.NONE, PREF_ID, Menu.NONE, "Preferences")
                .setIcon(R.drawable.preferences).setAlphabeticShortcut('e');

        return (super.onCreateOptionsMenu(menu));*/
        MenuInflater inflater = getMenuInflater();      
        inflater.inflate(R.menu.activity_ygo_main, menu);
        return true;        
    }

回答1:


First of all I want to say : Say Goodbye to the Menu Button

Your code have no problem, and it should be showing the icons if the drawables are there in correct folder,Working fine on Android 2.2.

The Menu features says :

   1.  Context menus: Do not support item shortcuts and item icons.
   2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged.
   3. Sub menus: Do not support item icons, or nested sub menus. 

No problem with your code, Problem may be with the API level you are using, but still want to suggest that don't use Menu anymore.


Android no longer requires a dedicated Menu button, some devices don’t have one, and you should migrate away from using it.




回答2:


If you use some following attribute in manifest file then it's will be show your icon....

<activity android:name=".ui.CategoryActivity"
        android:label="@string/app_name"
        **android:theme="@android:style/Theme.NoTitleBar"**></activity>

It's work fine for me...:)

**must be enter.



来源:https://stackoverflow.com/questions/11716467/why-the-pictures-icon-dont-show-up-in-the-menu

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