android option menu with icon

前端 未结 9 1086
借酒劲吻你
借酒劲吻你 2021-02-05 08:36

How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.

Jav

相关标签:
9条回答
  • 2021-02-05 08:39

    Override OnPrepareOptionsMenu and add icon from there too

    and if its for above 3.0, use android:showAsAction in xml.

    eg. android:showAsAction="ifRoom|withText"

    0 讨论(0)
  • 2021-02-05 08:43

    To enable Option Menu with Icons:

    Wrap the Items with an Item with showAsAction="always" and a Menu:

    <?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:title="@string/title_menu"
            android:icon="@mipmap/ic_icon_menu"
            app:showAsAction="always">
            <menu>
                <item
                    android:id="@+id/action1"
                    android:orderInCategory="100"
                    android:title="@string/action1"
                    android:icon="@mipmap/ic_icon_action1"
                    app:showAsAction="never" />
            </menu>
        </item>
    </menu>
    
    0 讨论(0)
  • 2021-02-05 08:44

    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...:) +1 for my own effort...

    **must be enter.

    0 讨论(0)
  • 2021-02-05 08:45

    you can directly set this into the xml file.

      <item android:id="@+id/add_contacts"
      android:icon="@android:drawable/plus_icon"
      android:title="Add Contacts"/>
    
    0 讨论(0)
  • 2021-02-05 08:49

    Easiest way is to use the @drawable only when setting your menu item.

    OR

    Simply put the @drawable before the title declaration.

    <?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/addToFavorites"
      android:icon = "@drawable/ic_favorite_border_white_24dp"
      android:title = "Hello"
      app:showAsAction="always" />
    <item
      android:id      ="@+id/about"
      android:title   ="About"
      app:showAsAction="never" />
    </menu>  
    
    0 讨论(0)
  • 2021-02-05 08:53

    You can create a custom menu like this:

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

    And then inflate it

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.my_menu, menu);
        return true;
    }
    

    More on this here: http://developer.android.com/guide/topics/ui/menus.html#options-menu

    0 讨论(0)
提交回复
热议问题