Is it possible to have a action bar menu using the

2019-03-30 10:21发布

recently I've switched from the regular action bar implementation to the recently released appcompat implementation. My app made heavy use of the action bar to provide functionality. Since switching, on older spots APIs (less than 11) don't have any menu items. And newer APIs do, but they don't show the image like configured (if room|withText). Has anyone else experienced this or came up with any solutions?

2条回答
仙女界的扛把子
2楼-- · 2019-03-30 11:14

If there is a physical "Menu" button on device, it will show the contextual menu. If not, the menu item will be added to the ActionBar.

查看更多
Evening l夕情丶
3楼-- · 2019-03-30 11:18

I found out what was up, when using the appcompat library. You can create your menu just like normal.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
   return true;
}

But, in your menu xml files, add a xmlns:app attribute to the menu tag, like so:

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

then, in each of your menu items, where you usually specify the "showAs" style (ifRoom, withText, etc.), include this alternative line alongside the regular one:

app:showAsAction="ifRoom|withText"
android:showAsAction="ifRoom|withText"

After this, your menus will show correctly on both current and old APIs. I got this information from here.

查看更多
登录 后发表回答