How to set visibility for an actionbar menu group?

巧了我就是萌 提交于 2020-06-25 08:36:12

问题


UPDATE

Originally I was using ActionBarSherlock I have since created a brand new project using a native android action bar just to test this and I am still getting the same problem.

I am successfully showing/hiding items but not groups. I am rapidly coming to the conclusion that there is a bug in the ActionBar and it is not possible to programmatically set the visibility of a group

END of UPDATE

Given the following menu When accessing the Group I get a null pointer exception

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

    <item android:id="@+id/action_settings"
        android:title="@string/settings"
        android:orderInCategory="100"
        android:showAsAction="never"/>

        <group android:id="@+id/mnu_text_group"
           android:visible="false">
            <item android:id="@+id/mnu_text_type"
                android:enabled="true" 
                android:visible="true"
                android:icon="@drawable/ic_action_text_icon"
            android:showAsAction="always"> 
            </item>
            <item android:id="@+id/text_color"
                android:enabled="true" 
                android:visible="true" 
                android:showAsAction="always" 
                android:icon="@drawable/ic_action_color_line">
            </item>
        </group>        
    <item android:id="@+id/mnu_images"
...

In the onPrepareOptionsMenu of the relevant activity I have

public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
    mnuImage.setEnabled(mEnableImageMenu);
    mnuTextGroup.setVisible(false);
    ...

The call to mnuTextGroup.setVisible(false); raises a null pointer exception

However, by changing the find method to find an item within the group works fine e.g. MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_type);but obviously this works just for the specific item. I know groups are designed for exactly this purpose, to be able to set the visibility of and enable/disable all items within the group but I have been unable to find a way to do this programatically.


回答1:


Finally found the solution I needed to use the setGroupVisible() method of the menu object passed into the onPrepareOptionsMenu() method

This is what worked for me

Instead of

MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
mnuImage.setEnabled(mEnableImageMenu);
mnuTextGroup.setVisible(false);

This is what I needed

menu.setGroupVisible(R.id.mnu_text_group, false);



回答2:


Simple and one line

navigationView.getMenu().setGroupVisible(R.id.groupstaff, false);



回答3:


Use In Activity Where You Want to Hide

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getSupportMenuInflater().inflate(R.menu.main_itemlist, menu);
        boolean isdown = false;

        menu.findItem(R.id.addwork).setVisible(isdown);

        MenuItem mnuTextGroup = menu.findItem(R.id.mnu_text_group);
            mnuTextGroup.setVisible(isdown);

        return true;
    }



回答4:


altering the options menu needs to be done within onPrepareOptionsMenu or else it sometimes doesn't work (not sure exactly why, hopefully someone else can elaborate):

@Override public boolean onPrepareOptionsMenu(Menu menu) {
  super.onPrepareOptionsMenu(menu);
  // set visibility of menu items here
  return true;
}


来源:https://stackoverflow.com/questions/21587341/how-to-set-visibility-for-an-actionbar-menu-group

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