安卓系统 - 底部导航菜单所选项目的图标颜色因碎片而无法改变。

问题描述 投票:0回答:1

底部导航所选项目的颜色是 不变虽然我已经提供了控制颜色变化的可绘制文件。我已经尝试了这么多次,但我不能找到代码中的错误。

请帮助我

这是底部导航

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

这就是图标的颜色

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

编辑 :

当我删除这段代码时,它可以正常工作。

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

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

android android-studio android-layout android-xml bottomnavigationview
1个回答
1
投票

问题就在这里。

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

你可以检查 doc:

返回

boolean true显示为选中的项目,false显示为不应该被选中的项目。. 考虑将不可选择的项目预先设置为禁用,使其看起来不具有交互性。

同时在你布局使用。

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

移动选择器 res/color 文件夹。


0
投票

在你的代码中用true代替false。这样就可以了。

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

        //your code

        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.