why it only show 2 menu icon on the actionbar even though there are enough space for 3?

别来无恙 提交于 2019-12-25 03:27:40

问题


I have 3 menu icons on the menu bar, but everytime it only shows 2 icons, the last one is in nowhere. my questions are: 1. there is enough space for 3 icons, why only 2 are shown? 2. if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?

Below is my menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >



    <item android:id="@+id/refresh"
          android:icon="@drawable/ic_menu_refresh"
          android:showAsAction="ifRoom" />        
    <item android:id="@+id/add_homework"
          android:icon="@android:drawable/ic_menu_edit"
          android:showAsAction="ifRoom" />    
    <item android:id="@+id/set_groupid"
          android:icon="@android:drawable/ic_menu_preferences"
          android:showAsAction="ifRoom" /> 


</menu>

and this snippet is in my MainActivity

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

}

回答1:


I have 3 menu icons on the menu bar

For the purposes of this answer, I am assuming that by "menu bar" you are referring to the action bar.

but everytime it only shows 2 icons, the last one is in nowhere

The last one is available by pressing the MENU button, for devices (or emulators) that have an off-screen MENU button.

there is enough space for 3 icons, why only 2 are shown?

Presumably because Android disagrees with your assessment of whether or not there is enough space for 3 icons.

if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?

I have no idea why you think forcing the 2nd icon -- which, by your admission, fits -- into the overflow menu would be a good idea. The 3rd menu item is in the overflow menu, which is accessed via the MENU button on devices that have one or a three-vertical-dots button on the action bar for devices that lack a MENU button.




回答2:


I encountered this same issue and resolved it by doing two things, although I think the main reason is the second item. I was using the Android Asset Studio to create icons for my Actionbar menu.

  1. In Android Asset Studio, I set the icon to "trim".

  2. In my menu/activity_main.xml definition file (or whatever you call your menu definition file) I defined my menu icons with android:showAsAction="always"

It looked something like this:

   <item android:id="@+id/menu_test"
     android:icon="@drawable/ic_menu_test"
     android:title="@string/menu_test"
     android:showAsAction="always" />


来源:https://stackoverflow.com/questions/13242538/why-it-only-show-2-menu-icon-on-the-actionbar-even-though-there-are-enough-space

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