如何更改选项卡文本指示器的颜色?我可以使用选择器标记更改图标,参考示例。但是无法更改文本颜色。怎么办?
在我进行一些网络搜索之后,我发现了来自Fred Grott(http://knol.google.com/k/fred-grott/advance-tabs/)的新答案。
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.color.text_tab_indicator));
R.color.text_tab_indicator是位于你的res/drawable文件夹中的选择器xml文件。
TextView
,可以通过从TabWidget
对象访问的View
对象检索到。
在您的自定义主题中进行样式更改
<item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item>
和
<style name="Widget.TabWidget">
<item name="android:textAppearance">@style/TextAppearance.Widget.TabWidget</item>
<item name="android:ellipsize">marquee</item>
<item name="android:singleLine">true</item>
</style>
<style name="TextAppearance.Widget.TabWidget">
<item name="android:textSize">14sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">@android:color/tab_indicator_text</item>
</style>
parent="@style/AppTheme"
来在styles.xml中使用现有的AppTheme样式。 - Gene BoDanny C的答案是100%正确的。我只想添加一些内容,以便使用资源文件制作完整答案。
在res/color文件夹下的text_tab_indicator文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:textColor="@color/text_tab_selected"
android:state_selected="true" />
<item android:textColor="@color/text_tab_unselected"
android:state_selected="false" />
</selector>
这个 text_tab_unselected & text_tab_selected 在 colors/values 文件夹下会看起来像这样
<resources>
<color name="text_tab_selected">#ffffff</color>
<color name="text_tab_unselected">#95ab45</color>
最后,在选项卡类文件中添加Dannyy的答案。
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.color.text_tab_indicator));
android:textColor
еә”иҜҘж”№дёәandroid:color
гҖӮ - iStar<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/text_tab_selected" />
<item android:state_selected="false" android:color="@color/text_tab_unselected" />
</selector>
将TabWidget的样式设置为指向xml代码中的特定样式:
<TabWidget
...
style="@style/TabText"
/>
<style name="TabText">
<item name="android:textColor">@color/tab_text_color</item>
</style>
对我来说,它像魔法一样奏效。
谢谢, Randall