首页 > 解决方案 > 底部导航视图中选定选项卡的颜色保持不变

问题描述

我正在向项目中添加一个 BottomNavigationView,并且我希望为选定的选项卡设置不同的文本(和图标色调)颜色(以实现使非选定选项卡变灰的效果)。我使用了一个带有 android:state_checked="true" 的颜色选择器资源文件,它工作正常,直到我将 BottomNavigationView.OnNavigationItemSelectedListener 实现到我的 Fragment 类之一。添加该侦听器后,默认选择的选项卡仍然突出显示,即使我选择了不同的选项卡。我尝试了状态选择器的不同属性,但没有任何帮助。

这是我将视图添加到布局的方法:

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/profileNavBar"
        android:layout_alignParentTop="true"
        android:background="@color/colorLavander"
        app:itemTextColor="@color/nav_item_color"
        app:itemIconTint="@color/nav_item_color"
        app:menu="@menu/profile_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.design.widget.BottomNavigationView>

这是我的颜色选择器 .xml 文件:

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

这是我的片段类:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


import org.home.com.gvwrev.R;


public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        BottomNavigationView bottomNavigationView = view.findViewById(R.id.profileNavBar);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        displayRevFragment(new RecordRevFragment());
    }

    public void displayRevFragment(Fragment fragment) {
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.revenueContainer, fragment)
                .commit();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        switch (item.getItemId()) {
            case R.id.itemRecord:
                fragment = new RecordRevFragment();
                break;
            case R.id.itemModify:
                fragment = new ModifyRevFragment();
                break;
            case R.id.itemView:
                break;
        }
        if (fragment != null) {
            displayRevFragment(fragment);
        }
        return false;
    }
}

请帮忙。

标签: javaandroidcolors

解决方案


问题是你应该return trueonNavigationItemSelected方法中选择一个项目时。尝试这个:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Fragment fragment = null;
    switch (item.getItemId()) {
        case R.id.itemRecord:
            fragment = new RecordRevFragment();
            break;
        case R.id.itemModify:
            fragment = new ModifyRevFragment();
            break;
        case R.id.itemView:
            break;
    }
    if (fragment != null) {
        displayRevFragment(fragment);
        return true;
    }
    return false;
}

推荐阅读