如何从菜单编程方式删除的MenuItems? [英] How to remove MenuItems from Menu programmatically?

查看:141
本文介绍了如何从菜单编程方式删除的MenuItems?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一些Android应用程序,和我有一些code菜单:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目
        机器人:showAsAction =ifRoom
        机器人:ID =@ + ID / menuItemToLeft
        机器人:图标=@绘制/ to_left/>
    <项目
        机器人:showAsAction =ifRoom
        机器人:ID =@ + ID / menuItemToRight
        机器人:图标=@绘制/ to_right/>
&所述; /菜单>

我用showAsAction,以显示在操作栏这个项目。另外我有3个选项卡进行导航。但有以下任务:删除(或设置可见为假),从操作栏这个项目选择与0位置选项卡时。但我不明白我怎么能做到这一点:

 公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
    mViewPager.setCurrentItem(tab.getPosition());
    如果(tab.getPosition()== 0){
    //一些code
    }
}


解决方案

尝试使用只是没有告诉他们:

  @覆盖
prepareOptionsMenu(菜单菜单)上公共布尔{
    super.on prepareOptionsMenu(菜单);
    menu.findItem(R.id.menuItemToLeft).setVisible(真);
    menu.findItem(R.id.menuItemToRight).setVisible(假);
    返回true;
}
// true或false根据您的要求

或删除:

  menu.removeItem(X); //其中x是菜单项的编号从0,1,...

您可能会需要重新创建使用菜单项menu.Add()

I'm developing some Android application, and I've got some code for menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:showAsAction="ifRoom"
        android:id="@+id/menuItemToLeft"
        android:icon="@drawable/to_left" />
    <item
        android:showAsAction="ifRoom"
        android:id="@+id/menuItemToRight"
        android:icon="@drawable/to_right"/>
</menu>

I use "showAsAction" in order to show this items on Action Bar. Also I've got 3 tabs for navigation. But there is the following task: remove (or set visibility as false) this items from Action bar when tab with 0 positions is selected. But I don't understand how I can do it:

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
    if (tab.getPosition()==0) {
    //some code
    }
}

解决方案

Try just not show them using:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.menuItemToLeft).setVisible(true);
    menu.findItem(R.id.menuItemToRight).setVisible(false);
    return true;
}
//true or false depending on your requirements

or to delete:

menu.removeItem(x); //where x is the number of the menu item from 0,1,...

You may then need to create the MenuItem again using menu.Add()

这篇关于如何从菜单编程方式删除的MenuItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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