How to disable menu item of bottom navigation view?

為{幸葍}努か 提交于 2021-02-17 02:07:30

问题


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)
}
...

回答1:


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
}



回答2:


Adding to mightyWOZ answer

navView.menu.findItem(R.id.navigation_home).isCheckable = false

this makes the menu item disabled (grey).



来源:https://stackoverflow.com/questions/59700294/how-to-disable-menu-item-of-bottom-navigation-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!