Overflow Actions on ActionBar not showing

人盡茶涼 提交于 2019-12-12 08:07:19

问题


I have an ActionBar using ActionBar Sherlock where I need it to display overflow because I have more actions than room. But, it doesn't show the overflow icon. Here is my configuration:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
      android:icon="@drawable/action_search"
      android:title="@string/menu_search"
      android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/menu_library"
      android:icon="@drawable/hardware_headphones"
      android:title="@string/my_music"
      android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/menu_downloads"
      android:icon="@drawable/av_download"
      android:title="@string/downloads"
      android:showAsAction="ifRoom|withText"/>
</menu>

And here is code to set it up:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getSupportMenuInflater();
    menuInflater.inflate(R.menu.shopping_menu, menu);
    MenuItem searchMenuItem = menu.findItem(R.id.menu_search);
    searchMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            startActivity(new Intent(ShopActivity.this, SearchDialog.class));
            return false;
        }
    });
    MenuItem downloadMenuItem = menu.findItem(R.id.menu_downloads);
    downloadMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            startActivity( new Intent(ShopActivity.this, DownloadQueueActivity.class) );
            return false;
        }
    });
    MenuItem myMusicItem = menu.findItem(R.id.menu_library);
    myMusicItem.setOnMenuItemClickListener( new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            startActivity(new Intent(ShopActivity.this, MyMusicActivity.class));
            return false;
        }
    });

    return true;
}

I've looked over the demos in ActionBar Sherlock, but I can't tell what they do differently to get the overflow than what I'm doing. So what's happening here why its not showing?


回答1:


If you have a physical menu key, the overflow indicator does not show. That is a behaviour by design. See here for more details on another question asked.




回答2:


Hmm I think there are two issues here. First, as t0mm13b states, if the device has a physical menu key, the overflow indicator does NOT show. This is by design. Although in my experience, it doesn't apply to every device (unfortunately...)

The second issue is that, if you want to force an item to the overflow, you need to set the showAsAction to "never". Otherwise, the only elements that appear in the overflow are ones that simply "don't fit" in the action bar. And given that you have 3 items that you want to display with text.. you're pretty much guranteed to have at least one overflow item, and therefore the overflow icon (with the caveat of the first paragraph)




回答3:


Try by changing the android:showAsAction tag to app:showAsAction according to the Android guide in the menu_main.xml file as shown below

Add this line if not present

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

    <item android:id="@+id/action_search"
        android:icon="@drawable/search_icon"
        android:title="@string/action_search"
        app:showAsAction="always"/> <!--change here android: to app:-->

    <item android:id="@+id/action_location"
        android:icon="@drawable/location_icon"
        android:title="@string/action_locate"
        app:showAsAction="always"/>
</menu>


来源:https://stackoverflow.com/questions/11480302/overflow-actions-on-actionbar-not-showing

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