我一直在搜索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