本文翻译自:How do I hide a menu item in the actionbar?
I have an action bar with a menuitem. 我有一个带有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();
#1楼
参考:https://stackoom.com/question/irfn/如何隐藏操作栏中的菜单项
#2楼
Get a MenuItem pointing to such item, call setVisible on 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 MenuItem is not a regular view that's part of your layout. 更新: MenuItem不是布局的常规视图。 Its something special, completely different. 它的特殊之处,完全不同。 Your code returns null for item and that's causing the crash. 您的代码为item返回null ,这导致崩溃。 What you need instead is to do: 你需要做的是:
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()
#3楼
You can call this: 你可以这样称呼:
MenuItem item = menu.findItem(R.id.my_item);
item.setVisible(false);
Update: 更新:
Make sure your code doesn't returns null for item or it may crash the application. 确保您的代码不会为item返回null ,否则可能会导致应用程序崩溃。
#4楼
Yes. 是。
- You can set a flag/condition. 您可以设置标志/条件。
- Call
invalidateOptionsMenu()when you want to hide the option. 如果要隐藏选项,请调用invalidateOptionsMenu()。 This will callonCreateOptionsMenu(). 这将调用onCreateOptionsMenu()。 - In
onCreateOptionsMenu(), check for the flag/condition and show or hide it the following way: 在onCreateOptionsMenu(),检查标志/条件并以下列方式显示或隐藏它:
MenuItem item = menu.findItem(R.id.menu_Done); if (flag/condition)) { item.setVisible(false); } else { }
#5楼
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(...)的项目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. 在我的例子中,我隐藏了所有项目。
#6楼
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);
}
}
本文详细介绍了在Android应用中如何控制操作栏菜单项的可见性,包括使用MenuItem的setVisible方法,以及如何在onCreateOptionsMenu方法中进行条件判断来动态显示或隐藏菜单项。
1087

被折叠的 条评论
为什么被折叠?



