Is it possible to have a action bar menu using the

2019-03-30 10:55发布

问题:

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?

回答1:

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.



回答2:

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.