Android create custom overflow menu item

2020-02-10 04:12发布

I want to create a custom overflow menu item in my ActionBar in addition at the Setting item like described in the image below:

Action bar

But if there is few space in the ActionBar I don't want that the Item1 and Item2 go into the Setting item as overflow, but into "my overflow item".

this is my menu xml code:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:icon="@android:drawable/ic_menu_agenda"  
    android:title="Item1"
    android:showAsAction="ifRoom|withText" />

<item
    android:icon="@android:drawable/ic_menu_add"  
    android:title="Item2"
    android:showAsAction="ifRoom|withText" />

<item android:id="@+id/pick_action_provider"
    android:icon="@android:drawable/ic_menu_sort_by_size" 
    android:showAsAction="always"
    android:title="Overflow" >
     <menu>  
        <item android:id="@+id/action_sort_size"  
              android:icon="@android:drawable/ic_menu_camera"  
              android:title="Item3" />  
        <item android:id="@+id/action_sort_alpha"  
              android:icon="@android:drawable/ic_menu_sort_alphabetically"  
              android:title="Item4" />  
    </menu>  
</item>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/></menu>

8条回答
疯言疯语
2楼-- · 2020-02-10 04:44

The best way to do what you want to achieve is to inflate a custom ActionBar .

Do the following steps.

1)Create a XML RelativeLayout

2) Include a menu item and tag inside the XML

3) Inflate the action bar via the ActionBar class.

4) That's it you're done!!

Warning: Don't forget to use the compatibility library!! There are some dependencies in it for the above to work.

查看更多
迷人小祖宗
3楼-- · 2020-02-10 04:58

Try using

<item .... <any_damn_name>:showAsAction="ifRoom|withText" />

do not forget to add

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:<any_damn_name>="http://schemas.android.com/apk/res-auto"

And try to have separate menus for fragment and global (Settings in Global.xml menu file)

Make sure you inflate both in correct order.

查看更多
登录 后发表回答