如何禁用底部导航视图的菜单项? [英] How to disable menu item of bottom navigation view?

查看:74
本文介绍了如何禁用底部导航视图的菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用材料设计底部导航菜单项?我可以设置.isClickable = false,但这不会将菜单项显示为已禁用,类似于按钮.我无法执行.isEnabled,API将不允许它.

How can I disable a material design bottom navigation menu item? I can set .isClickable = false but this doesn't show the menu item as disabled, similar to a button. I can't do .isEnabled, the API won't allow it.

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/menu_item_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="@color/colorDark"
app:menu="@menu/bottom_navigation_menu">

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_home"
android:title="Home"
android:icon="@drawable/home_button"
app:showAsAction="ifRoom" />


class Something : AppCompatActivity() {
 private lateinit var mHomeBtn: BottomNavigationItemView
 override fun onCreate(...) {
  mHomeBtn = this.findViewById(R.id.action_home)
  mHomeBtn.isClickable = false <--- will make it unable to click but won't show disabled
  mHomeBtn.isEnabled = false <--- will throw an error
  mHomeBtn.setOnClickListener(this)
}
...

推荐答案

您应该获得BottomNavigationView的菜单,然后找到并禁用所需的MenuItem.在代码中,可以按以下步骤完成

You should get the menu of your BottomNavigationView and then find and disable the MenuItem that you want. In code it could be done as follows

override fun onCreate(savedInstanceState: Bundle?) {
    // Find the bottom navigation view, (Use correct ID)
    // menu_item_1 is probably not a good ID for a navigation view
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    // Find the menu item and then disable it
    navView.menu.findItem(R.id.navigation_home).isEnabled = false
}

这篇关于如何禁用底部导航视图的菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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