每次按下其中一个按钮时,我都会尝试更新我的应用程序的菜单按钮; 在API 11+中,您需要打电话invalidateOptionsMenu()来执行此操作; 因为我希望我的应用程序与较低的API兼容,所以在使用api 11+时如何才调用此方法?
我一直在搜索invalidateOptionsMenu(),我知道它的作用.但我想不出这个方法可能有用的任何现实生活中的例子.
我的意思是,例如,假设我们想要为我们添加一个新MenuItem的ActionBar,我们可以简单地从菜单中获取onCreateOptionsMenu(Menu menu)并在任何按钮的操作中使用它.
现在我真正的问题是,遵循唯一的使用方式invalidateOptionsMenu()?
bool _OtherMenu;
protected override void OnCreate (Bundle bundle)
{
_OtherMenu = false;
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate
{
if(_OtherMenu)
_OtherMenu = false;
else
_OtherMenu = true;
InvalidateOptionsMenu ();
};
}
public override bool OnCreateOptionsMenu (IMenu menu)
{
var inflater = this.SupportMenuInflater;
if(_OtherMenu)
inflater.Inflate (Resource.Menu.another_menu, menu);
else
inflater.Inflate (Resource.Menu.main_activity_menu, menu);
return base.OnCreateOptionsMenu (menu);
}
Run Code Online (Sandbox Code Playgroud)
单击按钮,将出现另一个菜单.再次单击该按钮,将出现上一个菜单.
PS对不起C#语法.
android android-menu android-optionsmenu android-actionbar xamarin