小编Ale*_* Ed的帖子

Android - 底部导航菜单所选项目的图标颜色由于碎片而没有改变

底部导航所选项目的颜色没有改变,尽管我提供了控制颜色变化的可绘制文件。我已经尝试了很多次,但我找不到代码中的错误。

请帮忙!

这是底部导航

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@color/bottomBackground"
        app:itemTextColor="@drawable/icon_color"
        app:itemIconTint="@drawable/icon_color"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_navigation_menu"/>
Run Code Online (Sandbox Code Playgroud)

这是 icon_color

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_checked="true"/>
    <item android:color="@color/grey" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

编辑 :

当我删除这段代码时它工作正常

        BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            if(id == R.id.navigation_library) {
                loadFragment(new LibraryFragment());
            }
            else if (id == R.id.navigation_for_you) {
                loadFragment(new ForYouFragment());
            }
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)

为什么这段代码会干扰我的功能。

android android-layout android-xml bottomnavigationview material-components

1
推荐指数
1
解决办法
4586
查看次数