android - 如何禁用底部导航 View 的菜单项?

标签 android kotlin material-design

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

<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">
菜单 XML
<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" />
Activity
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你要的那个。在代码中可以按如下方式完成

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
}

关于android - 如何禁用底部导航 View 的菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59700294/

相关文章:

javascript - 是否可以使用 Kotlin 构建完整的 Node.js Web 应用程序?

android - 如何将 NavArgs 映射到 NavArgument?

android - 如何在 RecyclerView 中使用 ItemAnimator?

angular - 如何更改angular4中md-input-container的下划线颜色?

java - 实时计算音频流中的音节

android - 有没有什么方法可以使用基于 ARM 的计算机为 Android 开发?

android - 即使我的应用程序关闭,广播接收器也可以工作

android - file.createNewFile() 给出 java.io.IOException : Not a directory

Android:带有自定义字体的文本周围的垂直空间

user-interface - 什么是最好的 Material Design UI 框架