Android ActionBar重新创建选项菜单 [英] Android ActionBar Recreate Options Menu

查看:224
本文介绍了Android ActionBar重新创建选项菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中使用ActionBar时,如何刷新选项菜单?我尝试隐藏和显示该栏,并使用"getSupportActionBar()"获取它的新实例

When using the ActionBar in Android, how do you refresh the options menu? I have tried hiding and showing the bar, along with getting a new instance of it with "getSupportActionBar()"

我正在尝试实现一个登录/注销"按钮,该按钮将根据用户的状态动态变化.

I am trying to implement a Login/Logout button that will change dynamically based on the state of the user.

这是我的onCreateOptionsMenu方法

Here is my onCreateOptionsMenu method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (loggedIn)
        menu.add(0, MENU2, 0, "Logout").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    else
        menu.add(0, MENU2, 0, "Login").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menu.add(0, MENU1, 0, "Home").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return super.onCreateOptionsMenu(menu);
}

谢谢!

推荐答案

使用

Invalidate the menu with invalidateOptionsMenu() and then put your code in the onPrepareOptionsMenu area.

这篇关于Android ActionBar重新创建选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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