Forcing Overflow menu button on Android

安稳与你 提交于 2019-12-07 14:37:51

问题


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:


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.



来源:https://stackoverflow.com/questions/10851462/forcing-overflow-menu-button-on-android

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