Android 如何隐藏操作栏中的菜单项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10692755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:37:57  来源:igfitidea点击:

How do I hide a menu item in the actionbar?

androidandroid-actionbarmenuitemandroid-menu

提问by Stir Zoltán

I have an action bar with a menuitem. How can I hide/show that menu item?

我有一个带有菜单项的操作栏。如何隐藏/显示该菜单项?

This is what I'm trying to do:

这就是我想要做的:

MenuItem item = (MenuItem) findViewById(R.id.addAction);
item.setVisible(false);
this.invalidateOptionsMenu();

回答by K-ballo

Get a MenuItempointing to such item, call setVisibleon it to adjust its visibility and then call invalidateOptionsMenu()on your activity so the ActionBar menu is adjusted accordingly.

获取MenuItem指向此类项目的指针,调用setVisible它以调整其可见性,然后调用invalidateOptionsMenu()您的活动,以便相应地调整 ActionBar 菜单。

Update:A MenuItemis not a regular view that's part of your layout. Its something special, completely different. Your code returns nullfor itemand that's causing the crash. What you need instead is to do:

更新:AMenuItem不是布局中的常规视图。它有些特别,完全不同。你的代码返回nullitem,这就是导致崩溃。你需要做的是:

MenuItem item = menu.findItem(R.id.addAction);

Here is the sequence in which you should call: first call invalidateOptionsMenu()and then inside onCreateOptionsMenu(Menu)obtain a reference to the MenuItem (by calling menu.findItem()) and call setVisible()on it

以下是您应该调用的顺序:首先调用invalidateOptionsMenu(),然后在内部onCreateOptionsMenu(Menu)获取对 MenuItem 的引用(通过调用menu.findItem())并调用setVisible()

回答by P1r4nh4

Found an addendum to this question:

找到了这个问题的附录:

If you want to change the visibility of your menu items on the go you just need to set a member variable in your activity to remember that you want to hide the menu and call invalidateOptionsMenu()and hide the items in your overridden onCreateOptionsMenu(...)method.

如果您想随时随地更改菜单项的可见性,您只需要在您的活动中设置一个成员变量,以记住您要隐藏菜单并invalidateOptionsMenu()在您的重写onCreateOptionsMenu(...)方法中调用和隐藏项目。

//anywhere in your code
...
mState = HIDE_MENU; // setting state
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // inflate menu from xml
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.settings, menu);

    if (mState == HIDE_MENU)
    {
        for (int i = 0; i < menu.size(); i++)
            menu.getItem(i).setVisible(false);
    }
}

In my example I've hidden all items.

在我的示例中,我隐藏了所有项目。

回答by suhas_sm

Yes.

是的。

  1. You can set a flag/condition.
  2. Call invalidateOptionsMenu()when you want to hide the option. This will call onCreateOptionsMenu().
  3. In onCreateOptionsMenu(), check for the flag/condition and show or hide it the following way:
  1. 您可以设置标志/条件。
  2. invalidateOptionsMenu()当您想隐藏选项时调用。这将调用onCreateOptionsMenu().
  3. 在 中onCreateOptionsMenu(),检查标志/条件并按以下方式显示或隐藏它:
MenuItem item = menu.findItem(R.id.menu_Done);

if (flag/condition)) {
  item.setVisible(false); 
} else { }
MenuItem item = menu.findItem(R.id.menu_Done);

if (flag/condition)) {
  item.setVisible(false); 
} else { }

回答by elias

You can call this:

你可以这样称呼:

MenuItem item = menu.findItem(R.id.my_item);
item.setVisible(false);

Update:

更新:

Make sure your code doesn't returns nullfor itemor it may crash the application.

确保您的代码不会返回nullitem否则可能会导致应用程序崩溃。

回答by Suragch

I was looking for an answer with a little more context. Now that I have figured it out, I will add that answer.

我正在寻找具有更多上下文的答案。现在我已经弄清楚了,我将添加该答案。

Hide button by default in menu xml

默认情况下在菜单 xml 中隐藏按钮

By default the share button will be hidden, as set by android:visible="false".

默认情况下,共享按钮将隐藏,由 设置android:visible="false"

enter image description here

在此处输入图片说明

main_menu.xml

main_menu.xml

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

    <!-- hide share button by default -->
    <item
        android:id="@+id/menu_action_share"
        android:icon="@drawable/ic_share_white_24dp"
        android:visible="false"     
        android:title="Share"
        app:showAsAction="always"/>

    <item
        android:id="@+id/menu_action_settings"
        android:icon="@drawable/ic_settings_white_24dp"
        android:title="Setting"
        app:showAsAction="ifRoom"/>

</menu>

Show button in code

在代码中显示按钮

But the share button can optionally be shown based on some condition.

但是可以根据某些条件选择显示共享按钮。

enter image description here

在此处输入图片说明

MainActivity.java

主活动.java

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    MenuItem shareItem = menu.findItem(R.id.menu_action_share);

    // show the button when some condition is true
    if (someCondition) {        
        shareItem.setVisible(true);
    }

    return true;
}

See also

也可以看看

回答by hotzen

didn't work for me. I had to explicitly use onPrepareOptionsMenuto set an item invisible.

对我不起作用。我必须明确使用onPrepareOptionsMenu来将项目设置为不可见。

So use onCreateOptionsMenuto create the menu and onPrepareOptionsMenuto change visibility etc.

因此用于onCreateOptionsMenu创建菜单和onPrepareOptionsMenu更改可见性等。

回答by Akhila

Initially set the menu item visibility to false in the menu layout file as follows :

最初在菜单布局文件中将菜单项可见性设置为 false,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:visible="false"
        android:id="@+id/action_do_something"
        android:title="@string/txt_do_something"
        app:showAsAction="always|withText"
        android:icon="@drawable/ic_done"/>
</menu>

You can then simply set the visibility of the menu item to false in your onCreateOptionsMenu() after inflating the menu.

然后,您可以在对菜单进行充气后,在 onCreateOptionsMenu() 中简单地将菜单项的可见性设置为 false。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(menu,R.menu.menu);
    MenuItem item = menu.findItem(R.id.menuItemId);
    if (item != null){
        item.setVisible(false);
    }
}

回答by mohamad sheikhi

Try this:

尝试这个:

MenuItem myitem = menu.findItem(R.id.my_item);
myitem.setVisible(false);

回答by Bala Vishnu

This worked for me from both Activity and Fragment

这从 Activity 和 Fragment 都对我有用

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (menu.findItem(R.id.action_messages) != null)
        menu.findItem(R.id.action_messages).setVisible(false);
}

回答by Hiram

P1r4nh4 answer works fine, I just simplified it using a boolean flag:

P1r4nh4 答案工作正常,我只是使用布尔标志简化了它:

public int mState = 0; //at the top of the code
//where you want to trigger the hide action
mState = 1; // to hide or mState = 0; to show
invalidateOptionsMenu(); // now onCreateOptionsMenu(...) is called again
...

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // inflate menu from xml
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.settings, menu);

    if (mState == 1) //1 is true, 0 is false
    {
        //hide only option 2
            menu.getItem(2).setVisible(false);
    }
}