Android TabLayout选项卡文本颜色状态列表
我希望我的TabLayout选项卡根据选项卡状态具有三种不同的文本颜色: 默认值-红色 禁用-黄色 选定-绿色 我正在添加我的表格布局:Android TabLayout选项卡文本颜色状态列表,android,android-layout,tabs,android-tablayout,Android,Android Layout,Tabs,Android Tablayout,我希望我的TabLayout选项卡根据选项卡状态具有三种不同的文本颜色: 默认值-红色 禁用-黄色 选定-绿色 我正在添加我的表格布局: <android.support.design.widget.TabLayout android:id="@+id/tlBetTypes" style="@style/BetTabLayoutStyle" android:layout_width="match
<android.support.design.widget.TabLayout
android:id="@+id/tlBetTypes"
style="@style/BetTabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="38dp">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/single"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/multi"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/system"/>
</android.support.design.widget.TabLayout>
那么BetTabTexStyle:
但它不起作用
我还尝试从代码和set创建一个ColorStateList,set使用setTabTextColors
你能解释一下我做错了什么吗?你试过这个吗
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabTextColor="@color/defaultColor"
app:tabSelectedTextColor="@color/whenTabSelectedColor"/>
试试这个
<style name="BetTabLayoutStyle" parent="Widget.Design.TabLayout">
<item name="tabIndicatorHeight">0dp</item>
<item name="tabTextAppearance">@style/BetTabTexStyle</item>
<item name="tabBackground">@drawable/background_tab_bet_type</item>
</style>
解决此问题的两个步骤: 从代码中设置ColorStateList,因为TabLayout会重置构造函数中的文本外观,如果您使用xml将其膨胀
private void setupTabsStyle() {
int[][] states = new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{android.R.attr.state_enabled},
new int[]{-android.R.attr.state_enabled}
};
@ColorRes int[] colorRes = new int[]{
R.color.orange_1,
R.color.grey_16,
R.color.grey
};
@ColorInt int[] colors = new int[colorRes.length];
for (int i = 0; i < colorRes.length; i++) {
colors[i] = ContextCompat.getColor(getContext(), colorRes[i]);
}
ColorStateList colorList = new ColorStateList(states, colors);
mTabLayout.setTabTextColors(colorList);
}
是的,这对我来说不起作用。我需要三种状态,谢谢,但还是不行。只是想知道为什么这会起作用
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"
app:tabTextColor="@color/defaultColor"
app:tabSelectedTextColor="@color/whenTabSelectedColor"/>
<style name="BetTabLayoutStyle" parent="Widget.Design.TabLayout">
<item name="tabIndicatorHeight">0dp</item>
<item name="tabTextAppearance">@style/BetTabTexStyle</item>
<item name="tabBackground">@drawable/background_tab_bet_type</item>
</style>
private void setupTabsStyle() {
int[][] states = new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{android.R.attr.state_enabled},
new int[]{-android.R.attr.state_enabled}
};
@ColorRes int[] colorRes = new int[]{
R.color.orange_1,
R.color.grey_16,
R.color.grey
};
@ColorInt int[] colors = new int[colorRes.length];
for (int i = 0; i < colorRes.length; i++) {
colors[i] = ContextCompat.getColor(getContext(), colorRes[i]);
}
ColorStateList colorList = new ColorStateList(states, colors);
mTabLayout.setTabTextColors(colorList);
}
private void enableTab(int tabIndex, boolean doEnable) {
LinearLayout tabStrip = ((LinearLayout) mTabLayout.getChildAt(0));
LinearLayout tabView = (LinearLayout) tabStrip.getChildAt(tabIndex);
tabView.setEnabled(doEnable);
for (int i = 0; i < tabView.getChildCount(); i++) {
View childAt = tabView.getChildAt(i);
childAt.setEnabled(doEnable);
}
}