底部导航视图图标更改不起作用

Sid*_*Sid 4 android android-icons kotlin android-drawable bottomnavigationview

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

在此输入图像描述

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

在此输入图像描述

这是我的代码

底部导航菜单

<?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>
Run Code Online (Sandbox Code Playgroud)

<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" />
Run Code Online (Sandbox Code Playgroud)

和选择器

调度选择器

<?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>
Run Code Online (Sandbox Code Playgroud)

设置选择器

<?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>
Run Code Online (Sandbox Code Playgroud)

收藏夹选择器

<?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>
Run Code Online (Sandbox Code Playgroud)

家庭选择器

<?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>
Run Code Online (Sandbox Code Playgroud)

活动代码

    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)
Run Code Online (Sandbox Code Playgroud)

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

编辑:我正在使用以下绘图

在此输入链接描述

在此输入链接描述

Zai*_*ain 5

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

因此,要解决您的问题,您需要通过以下方式显式禁用此功能:

val btmNav = findViewById<BottomNavigationView>(R.id.nav_view)
navView.itemIconTintList = null
Run Code Online (Sandbox Code Playgroud)

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

这些图标在选中/未选中状态下已经是相同的图标,但具有不同的色调颜色。如果这些图标是矢量,那么您只需使用 中的选择器对颜色进行着色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>
Run Code Online (Sandbox Code Playgroud)

并应用:

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    app:itemIconTint="@drawable/icon_color_selector"
Run Code Online (Sandbox Code Playgroud)

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

<?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>
Run Code Online (Sandbox Code Playgroud)

更新:

当您使用导航架构组件时,请确保菜单中的 id与片段bottomNavView中相应的 id 相匹配。navGraphbottomNavView

如果您不使用 anavGraph作为bottomNavView片段,则无法使用navView.setupWithNavController(navController)

这是您的可绘制图标