我是android的新手,我最近研究了选项菜单的文档,我还没有理解 onPrerpareOptionMenu() 。在我的应用程序中单击按钮时,我想更改同一活动的选项菜单。
...谢谢
public boolean onPrepareOptionsMenu(Menu menu){
return false;
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if( )//condition
inflater.inflate(R.menu.view_record, menu);
else
inflater.inflate(R.menu.add_record, menu);
return super.onPrepareOptionsMenu(menu);
}
答案 0 :(得分:0)
我在我的几个相机应用程序中执行此操作,根据通过滑动控件启用的相机功能(虽然按钮也可用)来更改菜单,即用于捕获图像或视频。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem someThing1 = menu.findItem(R.id.someThing1);
MenuItem someThing2 = menu.findItem(R.id.someThing2);
MenuItem someThing3 = menu.findItem(R.id.someThing3);
MenuItem someThing4 = menu.findItem(R.id.someThing4);
MenuItem someThing5 = menu.findItem(R.id.someThing5);
MenuItem help = menu.findItem(R.id.help);
MenuItem about = menu.findItem(R.id.about);
if (some_condition) { //could be button state or..?
someThing1.setTitle(R.string.someThing1a);
someThing2.setTitle(R.string.someThing2a);
someThing3.setTitle(R.string.someThing3a);
someThing4.setVisible(false);
someThing5.setVisible(false);
help.setVisible(false);
about.setVisible(false);
} else {
someThing1.setTitle(R.string.someThing1b);
someThing2.setTitle(R.string.someThing2b);
someThing3.setTitle(R.string.someThing3b);
someThing4.setVisible(true);
someThing5.setVisible(true);
help.setVisible(true);
about.setVisible(true);
}
return true; // this is important to call so that new menu is shown
}