By default I set the visibility to false by using following code.
默认情况下,我使用以下代码将可见性设置为false。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
menu.findItem(R.id.action_share).setVisible(false);
return true;
}
Now how can I make it visible again when user clicks a button in my activity.
现在,当用户点击我的活动中的按钮时,如何让它再次可见。
3 个解决方案
#1
7
In your onCreateOptionsMenu:
在你的onCreateOptionsMenu中:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
if (hideIcon){
menu.findItem(R.id.action_share).setVisible(false);
}else{
menu.findItem(R.id.action_share).setVisible(true);
}
return true;
}
In method where you want to show/hide the icon, just set the boolean hideIcon to true or false and call :
在要显示/隐藏图标的方法中,只需将布尔值hideIcon设置为true或false,然后调用:
invalidateOptionsMenu();
to refresh the menu.
刷新菜单。
#2
1
get the instance of that menu item, and you can set its item Visibility every time.
获取该菜单项的实例,并且每次都可以设置其项目可见性。
Menu mMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
mMenu = menu;
mMenu.findItem(R.id.action_share).setVisible(false);
return true;
}
//somewhere else
mMenu.findItem(R.id.action_share).setVisible(true);
and based on @chol answer call invalidateOptionsMenu();
并基于@chol接听电话invalidateOptionsMenu();
#3
0
While you can create a class field
虽然您可以创建一个类字段
private Menu menu;
and capture its value in onCreateOptionsMenu()
并在onCreateOptionsMenu()中捕获其值
this.menu = menu;
Then Write the code in clickListener
然后在clickListener中编写代码
this.menu.findItem(R.id.action_share).setVisible(true);
#1
7
In your onCreateOptionsMenu:
在你的onCreateOptionsMenu中:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
if (hideIcon){
menu.findItem(R.id.action_share).setVisible(false);
}else{
menu.findItem(R.id.action_share).setVisible(true);
}
return true;
}
In method where you want to show/hide the icon, just set the boolean hideIcon to true or false and call :
在要显示/隐藏图标的方法中,只需将布尔值hideIcon设置为true或false,然后调用:
invalidateOptionsMenu();
to refresh the menu.
刷新菜单。
#2
1
get the instance of that menu item, and you can set its item Visibility every time.
获取该菜单项的实例,并且每次都可以设置其项目可见性。
Menu mMenu;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_items, menu);
mMenu = menu;
mMenu.findItem(R.id.action_share).setVisible(false);
return true;
}
//somewhere else
mMenu.findItem(R.id.action_share).setVisible(true);
and based on @chol answer call invalidateOptionsMenu();
并基于@chol接听电话invalidateOptionsMenu();
#3
0
While you can create a class field
虽然您可以创建一个类字段
private Menu menu;
and capture its value in onCreateOptionsMenu()
并在onCreateOptionsMenu()中捕获其值
this.menu = menu;
Then Write the code in clickListener
然后在clickListener中编写代码
this.menu.findItem(R.id.action_share).setVisible(true);