如何禁用底部导航视图的菜单项?

时间:2020-01-12 01:44:47

标签: android kotlin material-design

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

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

2 个答案:

答案 0 :(得分:2)

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

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
}

答案 1 :(得分:0)

添加到mightyWOZ答案

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

这会禁用菜单项(灰色)。