我已在我的应用程序中集成了底部栏导航栏。但是当我滑动时,选项卡的颜色不会改变。这很奇怪,因为我有选择器文件。有办法解决这个问题吗?
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;
}
});
选择器.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" />
更改为
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>
尽管阅读了所有答案,但我对整个过程感到困惑,所以这里是我如何逐步解决它,以便初学者可以正确理解它
假设您创建了带有底部导航的
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"
您必须将选择器设置为 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" />
我使用@Anurag Singh 的方式,但直到我删除此行后它才起作用:
navigation?.itemIconTintList = null
感谢我自己。
只需添加: item.setChecked(true);
每个箱子内;
在 BottomNavigationView 应用程序中:itemIconTint="" 提供颜色而不是选择器
下面的解决方案对我有用。
bottomNavigationView = findViewById(R.id.bottomNavView);
int[][] states = new int[][] {
new int[] { android.R.attr.state_selected},
new int[] {-android.R.attr.state_selected},
new int[] {-android.R.attr.state_checked},
new int[] { android.R.attr.state_checked}
};
int[] colors = new int[] {
ContextCompat.getColor(this,R.color.colorPrimary),
ContextCompat.getColor(this,R.color.gray),
ContextCompat.getColor(this,R.color.gray),
ContextCompat.getColor(this,R.color.colorPrimary)
};
ColorStateList myList = new ColorStateList(states, colors);
bottomNavigationView.setItemIconTintList(myList);//this for item icon
bottomNavigationView.setItemTextColor(myList);// this for item text
创建 ColorStateList 并以编程方式添加色调颜色