简体   繁体   中英

BottomNavigationView Original icon color

I have my bottomNavigationView :

在此处输入图片说明

And i added this class to prevent it from doing shiftingMode :

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 :

<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 .

底部导航视图

So we had to do this in addition:

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:

<?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.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM