如何隐藏操作栏中的菜单项?

编程入门 行业动态 更新时间:2024-10-28 14:27:10
本文介绍了如何隐藏操作栏中的菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

这就是我想要做的:

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

推荐答案

获取一个指向该 item 的 MenuItem,在其上调用 setVisible 调整其可见性,然后调用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.

更新: MenuItem 不是布局中的常规视图.它有些特别,完全不同.您的代码为 item 返回 null 并导致崩溃.你需要做的是:

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);

这是您应该调用的顺序:首先调用 invalidateOptionsMenu() 然后在 onCreateOptionsMenu(Menu) 中获取对 MenuItem 的引用(通过调用 menu.findItem())并调用setVisible() 就可以了

Here is the sequence in which you should call: first call invalidateOptionsMenu() and then inside onCreateOptionsMenu(Menu) obtain a reference to the MenuItem (by calling menu.findItem()) and call setVisible() on it

更多推荐

如何隐藏操作栏中的菜单项?

本文发布于:2023-11-16 19:31:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   菜单项   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!