为什么 BottomNavigationView 不更改选定的选项卡图标?

Why does BottomNavigationView not changing selected tab Icon?

我添加了 BottomNavigationView。当我点击任何选项卡时,只有 Fragment 被替换,它不显示新选择的选项卡图标和标题(主页选项卡始终显示为选中状态)有什么问题?

我的 XML 布局 - MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/FragmentContainerFrameLayout"
        android:layout_above="@id/bottomNavAppBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/bottomNavAppBar"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            app:labelVisibilityMode="unlabeled"
            android:id="@+id/bottomNavgView"
            app:menu="@menu/bottom_nav"
            android:background="?android:attr/windowBackground"
            app:itemTextColor="#000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.appbar.AppBarLayout>

</RelativeLayout>

我的 java 代码 - MainActivity.java

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    BottomNavigationView bottomNavigationView;
    Fragment selectedFragment;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView=findViewById(R.id.bottomNavgView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.homeMenu:
                selectedFragment=new HomeFragment();
                break;

            case R.id.searchMenu:
                selectedFragment=new SearchFragment();
                break;

            case R.id.addPostMenu:
                selectedFragment=null;
                startActivity(new Intent(MainActivity.this,AddPostActivity.class));
                break;

            case R.id.likesMenu:
                selectedFragment=new LikesFragment();
                break;

            case R.id.profileMenu:
                selectedFragment=new ProfileFragment();
                break;
        }

        if(selectedFragment!=null)
        {
            FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.FragmentContainerFrameLayout,selectedFragment);
            transaction.commit();
        }
        return false;
    }
}

彩色文件-@menu/bottom_nav

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:state_checked="true"/>
    <item android:color="@android:color/darker_gray"/>
</selector>

Gradle 文件

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

菜单文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/homeMenu"
        android:title="Home"
        android:icon="@drawable/ic_home"></item>

    <item
        android:id="@+id/searchMenu"
        android:title="Search"
        android:icon="@drawable/ic_search"></item>


    <item
        android:id="@+id/addPostMenu"
        android:title="Add Post"
        android:icon="@drawable/ic_add_post"></item>


    <item
        android:id="@+id/likesMenu"
        android:title="Likes"
        android:icon="@drawable/ic_like"></item>

    <item
        android:id="@+id/profileMenu"
        android:title="Profile"
        android:icon="@drawable/ic_profile"></item>

</menu>

我没有表现出世界上的东西,为什么会出现这种情况?请帮助我。

您必须 return trueonNavigationItemSelected 方法中:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
       ...
       return true;  
    }

您可以查看 doc:

Returns

boolean true to display the item as the selected item and false if the item should not be selected. Consider setting non-selectable items as disabled preemptively to make them appear non-interactive.

你也在使用app:labelVisibilityMode="unlabeled"
这意味着标签对所有导航项都是隐藏的(LABEL_VISIBILITY_UNLABELED)