如何在某些情况下将Menuitem隐藏在工具栏中?
我有一个表单屏幕,它也是一个信息屏幕,但在不同的状态下。当用户进入创建时,我想隐藏为删除的分配的menuitem。 如何在不破坏应用程序的情况下执行此操作? 我试图这样称呼它:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
I have a form screen that is also an information screen, but in different states. When the user enters to create I want to hide the MenuItem that assigns to delete.
How to do this without breaking the app?
I'm trying to call it like this:
val menu = findViewById<MenuItem>(R.id.deleteBarra)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 docs>
inprepareoptionsmenu
中的设置,而不是onCreateeOptionsMenu
。onprepareoptionsmenu
每次菜单都需要显示(而不是在设置期间)时都会调用。因此,您可以设置布尔值或任何内容是否应显示该项目,并调用
invalidateOptionsMenu()
对其进行重新播放。在prepareoptionsmenu()
您可以访问菜单本身,因此您可以检查布尔值并在适当的项目上设置可见性Here's the docs about it, but you basically want to do your menu setup in
onPrepareOptionsMenu
instead ofonCreateOptionsMenu
.onPrepareOptionsMenu
gets called every time the menu needs to be displayed (instead of once, during setup).So you can set a boolean or whatever to say whether the item should be shown, and call
invalidateOptionsMenu()
to redisplay it. InprepareOptionsMenu()
you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item在您希望隐藏的条件下,您可以在该条件下添加它。
或者
Under what condition you want it to be hidden, you can add it under that condition.
or