如何隐藏在动作条菜单项? [英] How do I hide a menu item in the actionbar?

查看:153
本文介绍了如何隐藏在动作条菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单项的操作栏。如何隐藏/显示该菜单项?

这是我想要做的:

 菜单项项=(菜单项)findViewById(R.id.addAction);
item.setVisible(假);
this.invalidateOptionsMenu();
 

解决方案

获取一个菜单项指向这样的项目,调用的setVisible 它来调整其知名度,然后调用 invalidateOptionsMenu()您的活动,以便在动作条菜单进行相应的调整。

更新: A 菜单项是不是正规认为是布局的一部分。它的一些特别的东西,完全不一样。您的code返回项目而这导致崩溃。你需要,而不是有什么做的:

 菜单项项= menu.findItem(R.id.addAction);
 

I have an action bar with a menuitem. How can I hide/show that menu item?

This is what I'm trying to do:

MenuItem item = (MenuItem) findViewById(R.id.addAction);
item.setVisible(false);
this.invalidateOptionsMenu();

解决方案

Get a MenuItem pointing to such item, call setVisible on it to adjust its visibility and then call invalidateOptionsMenu() on your activity so the ActionBar menu is adjusted accordingly.

Update: A MenuItem is not a regular view that's part of your layout. Its something special, completely different. Your code returns null for item and that's causing the crash. What you need instead is to do:

MenuItem item = menu.findItem(R.id.addAction);

这篇关于如何隐藏在动作条菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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