Android-正确使用invalidateOptionsMenu() [英] Android - Correct use of invalidateOptionsMenu()

查看:562
本文介绍了Android-正确使用invalidateOptionsMenu()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 invalidateOptionsMenu()上进行了大量搜索,我知道它的作用。但是我想不出任何现实的例子,这种方法可能有用。

I have been searching a lot on invalidateOptionsMenu() and I know what it does. But I cannot think of any real life example where this method could be useful.

例如,我的意思是说我们想添加一个新的 MenuItem 到我们的 ActionBar ,我们可以简单地从 onCreateOptionsMenu(菜单菜单)并在任何按钮的操作中使用它。

I mean, for instance, let's say we want to add a new MenuItem to our ActionBar, we can simply get the Menu from onCreateOptionsMenu(Menu menu) and use it in any button's action.

现在我真正的问题是,遵循使用 only 方法invalidateOptionsMenu()

Now to my real question, is following the only way of using 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);
}

单击按钮,出现另一个菜单。再次单击该按钮,将显示上一个菜单。

Click the button and a different menu appears. Click the button again and previous menu appears.

P.S。抱歉C#语法。

P.S. Sorry for the C# syntax.

推荐答案

invalidateOptionsMenu()用于例如Android,菜单内容已更改,应重新绘制菜单。例如,您单击一个在运行时添加另一个菜单项或隐藏菜单项组的按钮。在这种情况下,您应该调用 invalidateOptionsMenu(),以便系统可以在UI上重绘它。该方法是OS调用 onPrepareOptionsMenu()的信号,您可以在其中实现必要的菜单操作。
此外, OnCreateOptionsMenu()在活动(片段)创建期间仅被调用一次,因此该方法无法处理运行时菜单更改。

invalidateOptionsMenu() is used to say Android, that contents of menu have changed, and menu should be redrawn. For example, you click a button which adds another menu item at runtime, or hides menu items group. In this case you should call invalidateOptionsMenu(), so that the system could redraw it on UI. This method is a signal for OS to call onPrepareOptionsMenu(), where you implement necessary menu manipulations. Furthermore, OnCreateOptionsMenu() is called only once during activity (fragment) creation, thus runtime menu changes cannot be handled by this method.

所有内容都可以在文档中找到


在系统调用onCreateOptionsMenu()之后,它将保留您填充的Menu实例
并将不要再次调用onCreateOptionsMenu()
,除非由于某种原因使菜单无效。但是,您应该
仅在活动生命周期中使用onCreateOptionsMenu()创建初始菜单状态,而不要使用
进行更改。

After the system calls onCreateOptionsMenu(), it retains an instance of the Menu you populate and will not call onCreateOptionsMenu() again unless the menu is invalidated for some reason. However, you should use onCreateOptionsMenu() only to create the initial menu state and not to make changes during the activity lifecycle.

想要基于活动生命周期中发生的事件
修改选项菜单,可以在
onPrepareOptionsMenu()方法中进行操作。此方法将当前存在的Menu对象
传递给您,以便您可以对其进行修改,例如添加,删除或
禁用项。 (片段还提供了onPrepareOptionsMenu()
回调。)

If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)

在Android 2.3.x及更低版本上,系统每次都会调用onPrepareOptionsMenu()
用户打开选项菜单(按菜单按钮)。

On Android 2.3.x and lower, the system calls onPrepareOptionsMenu() each time the user opens the options menu (presses the Menu button).

在Android 3.0及更高版本上,当菜单项出现时,选项菜单始终被视为
在操作栏中显示。当事件
发生并且您要执行菜单更新时,必须调用
invalidateOptionsMenu()来请求系统在PrepareOptionsMenu()上调用

On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the action bar. When an event occurs and you want to perform a menu update, you must call invalidateOptionsMenu() to request that the system call onPrepareOptionsMenu().

这篇关于Android-正确使用invalidateOptionsMenu()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆