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

查看:23
本文介绍了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.

我的意思是,例如,假设我们想向我们的 ActionBar 添加一个新的 MenuItem,我们可以简单地从 onCreateOptionsMenu(Menu menu) 并在任何按钮的操作中使用它.

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.

现在我的真正问题是遵循唯一使用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.

附言抱歉 C# 语法.

P.S. Sorry for the C# syntax.

推荐答案

invalidateOptionsMenu() 用于表示Android,菜单内容发生变化,需要重新绘制菜单.例如,您单击一个按钮会在运行时添加另一个菜单项,或隐藏菜单项组.在这种情况下,您应该调用 invalidateOptionsMenu(),以便系统可以在 UI 上重新绘制它.此方法是操作系统调用 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.

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

All can be found in documentation:

系统调用onCreateOptionsMenu()后,保留了一个实例您填充的菜单,不会再次调用 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() 请求系统调用onPrepareOptionsMenu().

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天全站免登陆