单击导航抽屉中的操作栏上的双项

时间:2014-04-11 03:36:16

标签: android android-fragments android-actionbar navigation-drawer

我使用导航抽屉做了一个应用程序,当我显示"设置"时打开抽屉。菜单


当在我的容器中显示片段时,显示两个菜单"设置"和"搜索信息"。 http://saifulhq.wordpress.com/?attachment_id=242


但是当我点击标题上的后退按钮然后出现抽屉时,有3个项目,如图片"设置","设置","搜索信息"
http://saifulhq.wordpress.com/?attachment_id=243


我的问题是如何从片段中删除菜单项然后从抽屉中添加菜单项?
以下是抽屉上显示菜单项的代码

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // If the drawer is open, show the global app actions in the action bar.
    // See also
    // showGlobalContextActionBar, which controls the top-left area of the
    // action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }
    super.onCreateOptionsMenu(menu, inflater);
}

这是showGlobalContextActionBar()

/**
 * Per the navigation drawer design guidelines, updates the action bar to
 * show the global app 'context', rather than just what's in the current
 * screen.
 */
private void showGlobalContextActionBar() {
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setTitle(R.string.app_name);
}

1 个答案:

答案 0 :(得分:1)

为了使你正在做的工作,你必须将菜单创建移动到onPrepareOptionsMenu(),因为这是每次创建菜单时调用的方法,因为OnCreateOptionsMenu()只被调用一次。

要从菜单中删除菜单项,只需保存菜单并致电removeItem()

以下是参考http://developer.android.com/guide/topics/ui/menus.html#ChangingTheMenu

相关问题