底部导航视图中选定选项卡的颜色保持不变

时间:2018-10-18 15:10:06

标签: java android colors

我正在将BottomNavigationView添加到项目中,并且我希望所选标签具有不同的文本(和图标色调)颜色(以实现未选中的标签变灰)。我将颜色选择器资源文件与android:state_checked =“ true”一起使用,并且在我对我的Fragment类之一实现BottomNavigationView.OnNavigationItemSelectedListener之前,它工作得很好。添加该侦听器后,即使我选择其他选项卡,默认情况下选中的选项卡仍会突出显示。我为状态选择器尝试了不同的属性,但没有任何帮助。

这是将视图添加到布局中的方式:

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

这是我的Fragment类:

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

请帮助。

1 个答案:

答案 0 :(得分:0)

问题在于,在return true方法中选择一项时,您应该onNavigationItemSelected。试试这个:

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