【发布时间】:2018-09-10 10:40:57
【问题描述】:
我正在尝试通过代码更改 MenuItem 的文本和图标颜色。我已经搜索了如何做到这一点的解决方案,文本正在更改,但图标没有。
这是我目前的代码:
public void setItemOptionColor(boolean isActive){
MenuItem menuItem = mDrawerNavigationView.getMenu().findItem(R.id.my_item);
SpannableString spannableString = new SpannableString(menuItem.getTitle());
Drawable drawable = menuItem.getIcon();
if(isActive){
spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.accent)), 0, spannableString.length(), 0);
DrawableCompat.setTint(drawable,getResources().getColor(R.color.accent));
//drawable.setColorFilter(getResources().getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);
//drawable.setTint(getResources().getColor(R.color.accent));
}
menuItem.setIcon(drawable);
menuItem.setTitle(spannableString);
}
所以现在要更改我正在使用的图标颜色DrawableCompat,但我的另外两个尝试也被注释掉了。所有这 3 种方法都不起作用。
我还要补充一点,即使我从 xml 文件中删除了将默认色调设置为白色,即使可绘制文件最初是黑色的,图标仍然显示为白色。而且我不知道为什么会这样,也许它与问题有关
【问题讨论】:
标签: android android-layout android-menu