BottomNavigationView Original icon color 我在这里发现它可以保留相同的图标颜色。但是,是否可以仅在选择图标时保留它并为未选择的选项卡保留不同的灰色? 这是我更改图标颜色的代码。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:color="@color/colorSecondary"
android:state_checked="true"/>
<item
android:color="#bebebe"
android:state_checked="false"/>
这里在state_checked中我尝试给@null,但它只是让图标变成粉红色。我想在选中时保留图标颜色。可能吗?
注意:我也尝试过使用bNavigationView.setIconTintList(null);这会将所有图标颜色更改为原始颜色。
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
BottomNavigationView bNavigationView;
BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_account:
// AS gives me an error here saying setItemIconTintList cannot be applied to (int,null)
bNavigationView.setItemIconTintList(0,null);
fragmentTransaction.replace(R.id.content,new AccountFragment()).commit();
break;
case R.id.navigation_technical:
fragmentTransaction.replace(R.id.content,new TechnicalEventsFragment()).commit();
break;
case R.id.navigation_corporate:
fragmentTransaction.replace(R.id.content,new CorporateEventsFragment()).commit();
break;
case R.id.navigation_cultural:
fragmentTransaction.replace(R.id.content,new CulturalEventsFragment()).commit();
break;
case R.id.navigation_dashboard:
fragmentTransaction.replace(R.id.content,new DashboardFragment()).commit();
break;
}
return true;
}
};
答案 0 :(得分:0)
试试这个
<?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/colorSecondary"/>
<item android:color="#bebebe" />
</selector>
答案 1 :(得分:0)
我知道这是一个老问题但他们想要保留图标颜色,因为使用此方法并将其设置为null setItemIconTintList(ColorStateList tint) - 设置应用于菜单图标的色调。
这是最简单的尝试,让我知道
bNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bNavigationView.setItemIconTintList(null);
这是最优雅的超快方式。
注意:将setItemIconTintList设置为null将删除图标的整个色调颜色,但如果你需要此特效图标的效果 你可以在onNavigationItemSelected中做到这一点
bNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.iconOne:
bNavigationView.setIconTintList(0,null);
replaceFragment(FirstFragment.newInstance(),"FragmentOne");
break;
case R.id.mySpecificIcon:
//setIconTintList taking to params the first
//is the position of the icon wich is array starts from
//0
bNavigationView.setIconTintList(1,null);
replaceFragment(FirstFragment.newInstance(),"Fragment2");
// mainViewPager.setCurrentItem(3);
break;
}
return true;
}
})