android - 底部导航 View 图标更改不起作用

标签 android kotlin android-drawable bottomnavigationview android-icons

我想在切换项目时更改为底部导航 View 的图标。

enter image description here

对于选定的项目,我有浅蓝色图标和深蓝色图标。 我正在为每个导航项使用可绘制的选择器,但我看到像下面这样的图像,灰色为非 Activity 状态,蓝色为 Activity 状态

enter image description here

这是我的代码

底部导航菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home_icon_selector"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_scheduler"
        android:icon="@drawable/schelduler_icon_selector"
        android:title="@string/title_scheduler" />

    <item
        android:id="@+id/navigation_favourites"
        android:icon="@drawable/favourites_icon_selector"
        android:title="@string/title_favourites" />


    <item
        android:id="@+id/navigation_settings"
        android:icon="@drawable/settings_icon_selector"
        android:title="@string/title_settings" />
</menu>

首页

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/margin_20"
    android:layout_marginLeft="@dimen/margin_20"
    android:layout_marginRight="@dimen/margin_20"
    android:layout_marginEnd="@dimen/margin_20"
    android:layout_marginBottom="@dimen/margin_8"
    android:background="@drawable/bottom_navigation_background"
    android:elevation="8dp"
    app:itemIconTint="@drawable/bottom_navigation_color_selector"
    app:labelVisibilityMode="unlabeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

和选择器

调度器选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/qa_scheduler_inactive" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_scheduler_blue" android:state_checked="true"/>
</selector>

设置选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/qa_settings_inactive" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_settings_blue" android:state_checked="true"/>
</selector>

收藏夹选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/qa_favorites_inactive" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_favourites_blue" android:state_checked="true"/>
</selector>

首页选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/qa_home_inactive" android:state_checked="false"/>
    <item android:drawable="@drawable/ic_home" android:state_checked="true"/>
</selector>

Activity 代码

    val navView: BottomNavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.nav_host_fragment)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_scheduler, R.id.navigation_favourites, R.id.navigation_settings))
    //  setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

我在这里做错了什么?请帮助...

编辑: 我正在使用以下可绘制对象

enter link description here

enter link description here

最佳答案

这个问题的原因是 app:itemIconTint 总是有一个值,即使它没有被使用,它也采用主色/强调色的默认值。

因此,要解决您的问题,您需要明确禁用此功能:

val btmNav = findViewById<BottomNavigationView>(R.id.nav_view)
navView.itemIconTintList = null

虽然我确实推荐另一件事:

图标在选中/未选中状态下已经是相同的图标,但具有不同的色调。如果这些图标是矢量,那么您可以使用 app:itemIconTint 中的选择器对颜色进行着色,并使用单一版本的图标而无需复制资源:

icon_color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#2596CD" android:state_checked="true" />
    <item android:color="#84D0F4" android:state_checked="false" />
</selector>

并应用:

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    app:itemIconTint="@drawable/icon_color_selector"

并且只保留带有图标的菜单项而不是选择器,例如:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home_icon"  //<<<<< icon not selector
        android:title="@string/title_home" />

    ....

</menu>

更新:

当您使用导航架构组件时,请确保 bottomNavView 菜单中的 ID 与 bottomNavView 的 navGraph 中的相应 ID 相匹配 fragment 。

如果您不为 bottomNavView fragment 使用 navGraph,则不能使用 navView.setupWithNavController(navController)

这是你的可绘制图标

关于android - 底部导航 View 图标更改不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68830483/

相关文章:

scala - Kotlin 的扩展函数是如何工作的?

android - 如何使用 Kotlin 协程实现计时器

android - setBackgroundDrawable 无法正常工作

android - 是否可以在没有用户交互的情况下访问 android [enable() 或 disable()] 中的蓝牙?

android - Android 应用程序的自动更新永远不会发生

kotlin - 使用通用接口(interface)列表中的类

android - 带有椭圆形角的自动完成 TextView

android - 在 Android 中禁用 SlidingDrawer 下的图层

android - 从右到左的菜单项 Android

android - 如何在其中包含动态文本的图像,全部在可绘制对象中,例如 Google 日历应用程序上的 "today"操作项?