item-设置可见性

如果我们想要设置menu中item的可见行,有两种方式:

1.直接在menu的xml代码中设置

<menu>
<item android:id="@+id/action_hotknot"
            android:showAsAction="always"
            android:icon="@drawable/action_mode_hotknot"
            android:title="@string/hotknot"
< !--可见性设置-- 
            android:visible="true" />
</menu>

2.在代码中设置

它跟控件设置可见行不一样,控件设置的是

        TextView tv = new TextView(this);
        tv.setVisibility(View.GONE);

而menu中设置为

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem mItem;
        mItem = menu.getItem(R.id.action_settings);
        mItem.setVisible(false);
        return true;
    }
原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_menuitem_150324141.html