【发布时间】:2017-09-26 11:59:08
【问题描述】:
我有一个带有图标的 TabLayout,这些图标使用 textColorPrimary 主题属性着色以匹配主题(浅色或深色)。但是,当我将此颜色应用到 MainActivity 中的 TabLayout 图标时,来自不同活动的工具栏图标也会发生变化。
屏幕截图显示 TabLayout 中的图标与 Activity 中的图标颜色相匹配。但是活动图标应该是白色的。
设置 TabLayout 图标和文本颜色的代码:
ColorStateList colors;
if (Build.VERSION.SDK_INT >= 23) {
colors = getResources().getColorStateList(color.tablayout_icon_colors, getTheme());
}
else {
colors = getResources().getColorStateList(color.tablayout_icon_colors);
}
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
Drawable tabicon = tab.getIcon();
CharSequence tabtitle = tab.getText();
LinearLayout tabLayout2 = (LinearLayout)((ViewGroup) tabLayout.getChildAt(0)).getChildAt(tab.getPosition());
TextView tabTextView = (TextView) tabLayout2.getChildAt(1);
if (tabicon != null) {
tabicon = DrawableCompat.wrap(tabicon);
DrawableCompat.setTintList(tabicon, colors);
}
if (tabtitle != null) {
tabTextView.setTextColor(colors);
}
}
tablayout_icon_colors.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorAccent"
android:state_selected="true" />
<item android:color="?android:attr/textColorPrimary" />
</selector>
我在单独的 Activity 中为菜单充气的所有内容是这样的:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.preview, menu);
return true;
}
【问题讨论】:
标签: android android-toolbar android-tablayout