Menu Items are not showing on Action Bar

前端 未结 12 710
清歌不尽
清歌不尽 2020-11-29 20:57

I have made a completely new project. I have added items to the menu layout file. Those items do not show up on the action bar\'s right side. I remember that an

相关标签:
12条回答
  • 2020-11-29 21:27

    What worked for me was changing a property in menu.xml

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/gamingactivity_filter" android:showAsAction="ifRoom"   android:icon="@drawable/ic_filter"/> 
    

    this used to work in Material theme but didn't for AppCompat Theme & Activity so i changed how my menu.xml and add app:showAsAction= property under namespace `"http://schemas.android.com/apk/res-auto"

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:id="@+id/gamingactivity_filter" android:showAsAction="ifRoom"  app:showAsAction="ifRoom" android:icon="@drawable/ic_filter"/>
    

    from actvity inflate menu through OnCreateOptionsMenu(IMenu menu) and now from OnOptionsItemSelected() i can also get the selected item id as

     public override bool OnOptionsItemSelected(IMenuItem item)
            {
                    switch (item.ItemId)
                    {
                        case Resource.Id.gamingactivity_filter:
                            OnBackPressed();
                            break;
                    }
                }
                return true;
            }
    
    0 讨论(0)
  • 2020-11-29 21:32

    all answers above explained this issue well

    but can we override this limitation and have the overflow menu item?!!

    yes we can hack

     private void makeActionOverflowMenuShown() {
        //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }
    }
    

    and inside onCreate call it

    references: https://stackoverflow.com/a/13098824/4251431

    0 讨论(0)
  • 2020-11-29 21:33

    OR you could create a structure like:

    <?xml version="1.0" encoding="utf-8"?>
    <menu >
    <item
        android:id="@+id/menu_main"
        android:icon="@drawable/icon_menu"
        android:showAsAction="always"
        android:title="@string/menu_A"
        android:visible="true">
        <menu>
            <item
                android:id="@+id/menu_settings1"
                android:showAsAction="never"
                android:title="@string/menu_A1"/>
            <item
                android:id="@+id/menu_settings2"
                android:showAsAction="never"
                android:title="@string/menu_A2"/>
            <item
                android:id="@+id/menu_settings3"
                android:showAsAction="never"
                android:title="@string/menu_A3"/>
        </menu>
    </item>
    </menu>
    

    Where the icon_menu you can create a new icon set with the three points icon clipart.

    0 讨论(0)
  • 2020-11-29 21:34

    I had the same problem. And I did all of the things that were said above. My problem was I forgot to override onCreateoptionMenu(Menu menu).

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    0 讨论(0)
  • 2020-11-29 21:39

    Since you set the showAsAction attribute to never, then these menu items will never show as action views. Try this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:showAsAction="ifRoom|withText"
            android:title="@string/action_option1"/>
        <item
            android:id="@+id/action_settings34"
            android:orderInCategory="100"
            android:showAsAction="ifRoom|withText"
            android:title="@string/action_option2"/>
        <item
            android:id="@+id/action_settings3"
            android:orderInCategory="100"
            android:showAsAction="ifRoom|withText"
            android:title="@string/action_option3"/>
    
    </menu>
    
    0 讨论(0)
  • 2020-11-29 21:42

    After searching and reading a lot of answer, I found that there is another little thing that you should pay attention to.

    At least it was my problem and I didn't find an answer of any above. On your .XML file. Your android:showAsAction= shouldn't be form android package, it should be from app package like in the example below:

    app:showAsAction=
    

    Don't forget to import the app package.

    0 讨论(0)
提交回复
热议问题