BottomNavigationBar - 更改选项卡图标颜色

时间:2017-02-20 14:14:39

标签: android bottomnavigationview

我在我的应用上集成了Bottom Bar导航栏。但是当我滑动时,标签的颜色不会改变。因为我有选择器文件,这很奇怪。有什么想法解决这个问题吗?

Activity.java

BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.bottom_navigation);


    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.bb_menu_arac:
                            fragment = new AraclarimFragment();
                            break;
                        case R.id.bb_menu_yaklasan:
                            fragment = new YaklasanlarFragment();
                            break;
                        case R.id.bb_menu_yakin:
                            fragment = new Yakinimdakiler();
                            break;

                    }
                    final FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.main_container, fragment).commit();
                    return true;
                }


            });

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/beyaz" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>

activiy.xml

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/beyaz"
    app:itemTextColor="@color/beyaz"
    app:menu="@menu/bottombar_menu" />

4 个答案:

答案 0 :(得分:25)

更改为app:itemIconTint="@drawable/selector"

同时将您的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/beyaz" />
<item android:color="@color/colorPrimaryDark"  />
</selector>

答案 1 :(得分:3)

您必须将selector设置为BottomNavigationView的itemIconTint。像

这样的东西
 <android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/selector"
    app:itemTextColor="@color/beyaz"
    app:menu="@menu/bottombar_menu" />

答案 2 :(得分:2)

尽管阅读了所有答案,但我对整个过程感到困惑,所以我是如何逐步解决它的,以便初学者可以正确理解它

假设您使用底部导航创建了 MainActivity

在您的 drawable 文件夹中创建 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>

然后转到 activity_main.xml 布局并将此行添加到 BottomNavigationView

app:itemIconTint="@drawable/bottom_navigation_selector"

如果您还想相应地更改文本颜色,则还需要添加此行

app:itemTextColor="@drawable/bottom_navigation_selector"
    

答案 3 :(得分:0)

您需要在BottomNavigationView android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"中添加此内容 它将帮助您更改图标的颜色。