反正,
the documentation涵盖了所有的东西。
在运行时更改菜单项
Once the activity is created, the
onCreateOptionsMenu() method is called
only once, as described above. The
system keeps and re-uses the Menu you
define in this method until your
activity is destroyed. If you want to
change the Options Menu any time after
it’s first created, you must override
the onPrepareOptionsMenu() method.
This passes you the Menu object as it
currently exists. This is useful if
you’d like to remove, add, disable, or
enable menu items depending on the
current state of your application.
例如。
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (isFinalized) {
menu.getItem(1).setEnabled(false);
// You can also use something like:
// menu.findItem(R.id.example_foobar).setEnabled(false);
}
return true;
}
在Android 3.0及更高版本中,当菜单项在操作栏中显示时,选项菜单被视为始终打开。当事件发生并且您想要执行菜单更新时,您必须调用invalidateOptionsMenu()来请求系统调用onPrepareOptionsMenu()。