简体   繁体   English

BottomNavigationView 原始图标颜色

[英]BottomNavigationView Original icon color

I have my bottomNavigationView :我有我的底部导航视图:

在此处输入图片说明

And i added this class to prevent it from doing shiftingMode :我添加了这个类以防止它执行 shiftMode :

public class BottomNavigationViewHelper {
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                //item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }

I just want to show the original icons colors without this selector, how i can reach this ?我只想显示没有这个选择器的原始图标颜色,我怎么能达到这个?

my navigation xml :我的导航 xml :

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/my_navigation_item"
        android:layout_alignParentBottom="true"/>

你必须让你的项目图标 tint list null ,你从你的 bootomNav 到达它:

bottomNavigationView.setItemIconTintList(null);

如果您使用 Kotlin,您会怎么做 从您的主活动中找到您的导航 ID,然后在您的 mainActivity onCreat 中使用此代码

 my_navigation_item.itemIconTintList = null

Our use case was a multicolored icon when the tab is active and a grey one when it is not.我们的用例在选项卡处于活动状态时是一个多色图标,在非活动状态时是一个灰色图标。 Setting itemIconTintList to null made the inactive tab icons have the same colors as the active ones when using the material component BottomNavigationView .在使用材质组件BottomNavigationView时,将itemIconTintList设置为 null 可以使非活动选项卡图标与活动选项卡图标具有相同的颜色。

底部导航视图

So we had to do this in addition:所以我们还必须这样做:

menu_bottom_navigation.xml: menu_bottom_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/first_tab"
        android:enabled="true"
        android:icon="@drawable/ic_first_tab_selector"
        android:title="@string/first_tab_title"/>
    <item
        android:id="@+id/second_tab"
        android:enabled="true"
        android:icon="@drawable/ic_second_tab_selector"
        android:title="@string/second_tab_title"/>
</menu>

ic_first_tab_selector.xml: ic_first_tab_selector.xml:

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

Where ic_first_tab_inactive.xml is the drawable for your inactive icon and ic_first_tab_active.xml is the drawable for your active icon.其中ic_first_tab_inactive.xml是非活动图标的可绘制对象,而ic_first_tab_active.xml是活动图标的可绘制对象。

You are able to use app:itemIconTint from xmlns:app="http://schemas.android.com/apk/res-auto"您可以使用xmlns:app="http://schemas.android.com/apk/res-auto" app:itemIconTint

For example例如

<android.support.design.widget.BottomNavigationView
    //...
    app:itemIconTint="@color/gray_scale"
    //...
/>

It is handy when your drawables have different colors当您的可绘制对象具有不同颜色时,这很方便

The accepted answer worked but that method is now deprecated.接受的答案有效,但现在不推荐使用该方法。

The new way to enable the original icon colors is:启用原始图标颜色的新方法是:

bottomNavigation.setForceTint(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BottomNavigationView选中的原始图标颜色和未选中的颜色不同 - BottomNavigationView original icon color for checked and not checked different 更改 BottomNavigationView 中的图标颜色 - Change the icon color in a BottomNavigationView Android BottomNavigationView色调图标颜色 - Android BottomNavigationView tint icon color 如何使用渐变颜色在bottomNavigationView中选择颜色图标 - how to color icon selected in bottomNavigationView with gradient color Kotlin - 动态更改 bottomNavigationView 图标颜色 - Kotlin - Change bottomNavigationView icon color dynamically 更改 BottomNavigationView 单项图标和文本颜色 - Changing BottomNavigationView single item icon and text color 带有BottomNavigationView的BottomAppBar:无法更改图标和文本颜色 - BottomAppBar with BottomNavigationView: Cant change icon and text color 如何显示此图标的原始颜色 - How to Show original color of this icon 如何在BottomNavigationView中显示没有色调的菜单项图标 - How to show menu item icon without tint color in BottomNavigationView 有没有办法为Android的BottomNavigationView的选定状态设置不同的图标/颜色? - Is there a way to have different icon/color for selected state of Android's BottomNavigationView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM