簡體   English   中英

底部導航視圖中選定選項卡的顏色保持不變

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

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

請幫忙。

問題是,當在onNavigationItemSelected方法中選擇一個項目時,您應該return true 嘗試這個:

@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