如何在BottomNavigationView中显示没有颜色的菜单项图标 [英] How to show menu item icon without tint color in BottomNavigationView

查看:509
本文介绍了如何在BottomNavigationView中显示没有颜色的菜单项图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含三个项目的BottomNavigationView.其中之一是用户标签.

I have created a BottomNavigationView with three items. One of it was user tab.

来宾选项卡上有一个图像,但是正在使用TintColor,但我们看不到.

For the guest tab, there is an image but the TintColor is applying and we can't see that.

那么如何去除该特定项目的色泽呢?

So how to remove tint colour for that particular item?

我尝试过

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                            item.setIconTintList(null);

                        }

但是没有运气.它适用于以上api 26

But no luck. And it applies to above api 26

我的活动

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottomNavigationView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:itemIconTint="@drawable/bottom_color_state"
    app:itemBackground="@color/colorAccent"
    app:itemTextColor="@drawable/bottom_color_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/menu_bottom_navigation" />

bottom_color_state.xml

bottom_color_state.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/white" android:state_enabled="true" />
        <item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
        <item android:color="@color/white" android:state_selected="true" />
        <item android:color="@color/off_white" android:state_selected="false" />
        <item android:color="@color/white" android:state_checked="true" />
        <item android:color="@color/off_white" android:state_checked="false" />
        <item android:color="@color/off_white" />
    </selector>

预先感谢

推荐答案

似乎无法更改仅一个菜单项的色彩,因为BottomNavigationView将色彩应用于包装可绘制器中的列表中的每个项目.您需要从导航视图中删除色调列表,并分别在每个菜单项图标上设置色调列表.

It appears there's no way to change the tint of just one menu item because the BottomNavigationView applies the tint to every item in the list from a wrapper drawable. You'll need to remove the tint list from the nav view and set your tint list on each of your menu item icons individually.

navView.itemIconTintList = null

然后在要着色的每个菜单项图标中,在可绘制的矢量上设置颜色状态列表.

Then in each of the menu item icons you want to tint, set your color state list on the vector drawable.

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="@color/bottom_color_state"
        android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"/>
</vector>

我早在API 21时就测试了该解决方案.

I tested the solution as far back as API 21.

这篇关于如何在BottomNavigationView中显示没有颜色的菜单项图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆