web-dev-qa-db-ja.com

Androidスタジオの下部ナビゲーションバーで選択したアイコンの色を変更する方法

Android studioの下部のナビゲーションバーで項目を選択すると、選択された背景項目はvalues-> colors.xmlのprimarycolorと等しくなります。次に、同じではないこの色を変更します原色どうすれば変更できますか?

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        Fragment fragment;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_addpost:
                    fragment = new AddFragment();
                    loadFragment(fragment);
                    return true;
                case R.id.navigation_notifications:
//                    mTextMessage.setText(R.string.title_notifications);
                    return true;
                case R.id.navigation_profile:
                    fragment = new ProfileFragment();
                    loadFragment(fragment);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        loadFragment(new HomeFragment());
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setItemTextColor(ColorStateList.valueOf(Color.RED));
    }
3
Hasan Cheraghi

BottomNavigationViewで選択したタブアイコンの色を変更するには、セレクターを使用する必要があります。

作成bottom_navigation_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android">
<item Android:state_checked="true" Android:color="@color/yourSelectedColor" />
<item Android:color="@color/defaultColor"  />
</selector>

適用app:itemIconTint="@drawable/bottom_navigation_selector"xmlファイルのBottomNavigationViewに追加します。

4
Chandan Sharma
bottomNavigationView.setItemIconTintList(ColorStateList.valueOf(Color.parseColor("#3F51B5")));
0
Ahmed Belmekhfi

試して、

navigation.setItemIconTintList(Color.BLUE);

更新:

navigation.setItemIconTintList(Color.parseColor("#fafafa"));