简体   繁体   English

Android - 底部导航菜单所选项目的图标颜色不会因碎片而改变

[英]Android - Bottom Navigation menu selected item's icon color is not changing due to fragments

The color of the selected item of the Bottom Navigation is not changing , although i have provided the the drawable file which governs the changes of the color.底部导航的选定项目的颜色没有变化,尽管我提供了控制颜色变化的可绘制文件。 I have tried so may times but i can't find the mistake in the code.我已经尝试过很多次,但我在代码中找不到错误。

Please Help!请帮忙!

This is the Bottom Navigation这是底部导航

<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"/>

This is the icon_color这是 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>

Edit:编辑:

When i Remove this piece of code it works fine当我删除这段代码时它工作正常

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

Why does this code interfering with my feature.为什么此代码会干扰我的功能。

The issue is here:问题在这里:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
       ...
       return false;  //Use true in your case
    }

You can check the doc:您可以查看文档:

Returns退货

boolean true to display the item as the selected item and false if the item should not be selected . boolean如果该项目不应该被选择,则将项目显示为已选项目,则为 true Consider setting non-selectable items as disabled preemptively to make them appear non-interactive.考虑将不可选择的项目预先设置为禁用,以使它们看起来非交互。

Also in you layout use:同样在您的布局中使用:

<com.google.android.material.bottomnavigation.BottomNavigationView
        app:itemTextColor="@color/icon_color"

moving the selector in the res/color folder.res/color文件夹中移动选择器。

Replace false with true in your code.将代码中的 false 替换为 true。 It will work.它会起作用的。

navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        //your code

        return true;
    }

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

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