Forcing Overflow menu button on Android

2019-05-09 13:48发布

I am developing an application for Android 4.0.3. I have multiple fragments and I have one menu item displayed on each of them. I want to have an overflow menu button that will have 2 menu items. How do I force the appearance of the overflow menu and add menu items to it ? I am adding menu items by calling the onCreateOptionsMenu function in each fragment.

Any and all help would be appreciated. Thanks!

1条回答
Animai°情兽
2楼-- · 2019-05-09 14:33

To force into the overflow menu do this:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
   menu.add(0, 2, 0, "Item 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

   return true;
}

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW will force the item into either:

Android 3.0 + : The ActionBar as an overflow Android 2.3 - : The menu button

If you are wanting to recreate the menu you will need to call invalidateOptionsMenu(); This will re-create from OnCreateOptionsMenu.

查看更多
登录 后发表回答