【发布时间】:2020-07-28 12:08:52
【问题描述】:
我知道我们可以通过this 方式从 XML 中设置底部导航选择颜色
但我想知道如何通过我的 Activity 以编程方式更改它?
这是我在 Activity 的OnNavigationItemSelectedListener 中尝试的。
item.getIcon().setTint(ContextCompat.getColor(context, R.color.colorBrown));
还尝试像这样更改色调列表:
item.setIconTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorPrimaryBlue)));
这是我的代码的完整 sn-p:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
switch (item.getItemId()) {
case R.id.navigationTask:
item.getIcon().setTint(ContextCompat.getColor(context, R.color.colorBrown));
fragment = new MyTaskFragment();
break;
case R.id.navigationProfile:
item.setIconTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorPrimaryBlue)));
fragment = new ProfileFragment();
break;
case R.id.navigationRequest:
fragment = new RequestListFragment();
break;
case R.id.navigationMore:
fragment = new MoreFragment();
break;
}
loadFragment(fragment);
return true;
};
但这对我不起作用。任何有关如何以编程方式更改此设置的想法或参考链接都会对我有所帮助。
注意:我只想更改所选项目的图标和文本色调颜色。不是底部导航中的全部项目。
提前致谢。
【问题讨论】: