简体   繁体   English

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

[英]Selected tab's color in Bottom Navigation View remain unchanged

I'm adding a BottomNavigationView to a project, and I would like to have a different text (and icon tint) color for the selected tab (to achieve greying out non-selected tabs effect). 我将BottomNavigationView添加到项目中,并且希望所选标签具有不同的文本(和图标色调)颜色(以使未选中的标签变灰)。 I used a color selector resource file with android:state_checked="true" and it worked fine until I implemented the BottomNavigationView.OnNavigationItemSelectedListener to one of my Fragment class. 我将颜色选择器资源文件与android:state_checked =“ true”一起使用,并且在我对我的Fragment类之一实现BottomNavigationView.OnNavigationItemSelectedListener之前,它工作得很好。 After adding that Listener the tab which was selected by default, remains highlighted even if I select a different tab. 添加该侦听器后,即使我选择其他选项卡,默认情况下选中的选项卡仍会突出显示。 I tried different attributes to the state selector but nothing is helping. 我为状态选择器尝试了不同的属性,但没有任何帮助。

Here is how I add the view to my layout: 这是将视图添加到布局中的方法:

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

Here is my color selector .xml file: 这是我的颜色选择器.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>

And here is my Fragment class: 这是我的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;
    }
}

Please help. 请帮忙。

The problem is that you should return true when an item is selected in onNavigationItemSelected method. 问题是,当在onNavigationItemSelected方法中选择一个项目时,您应该return true Try this: 尝试这个:

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

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

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