如何在某些情况下将Menuitem隐藏在工具栏中?

发布于 2025-01-22 15:29:05 字数 178 浏览 2 评论 0原文

我有一个表单屏幕,它也是一个信息屏幕,但在不同的状态下。当用户进入创建时,我想隐藏为删除的分配的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一向肩并 2025-01-29 15:29:05

这是 docs> inprepareoptionsmenu中的设置,而不是onCreateeOptionsMenuonprepareoptionsmenu每次菜单都需要显示(而不是在设置期间)时都会调用。

因此,您可以设置布尔值或任何内容是否应显示该项目,并调用invalidateOptionsMenu()对其进行重新播放。在prepareoptionsmenu()您可以访问菜单本身,因此您可以检查布尔值并在适当的项目上设置可见性

Here's the docs about it, but you basically want to do your menu setup in onPrepareOptionsMenu instead of onCreateOptionsMenu. 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. In prepareOptionsMenu() you have access to the menu itself, so you can check that boolean and set the visibility on the appropriate item

糖粟与秋泊 2025-01-29 15:29:05

在您希望隐藏的条件下,您可以在该条件下添加它。

  menu.visibility = View.INVISIBLE

或者

   menu.visibility = View.GONE

Under what condition you want it to be hidden, you can add it under that condition.

  menu.visibility = View.INVISIBLE

or

   menu.visibility = View.GONE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文