![](/img/trans.png)
[英]Android how to dynamically change actionbar tabs text color on orientation change
[英]how to change the color of the tabs indicator text in android?
以下是我在一次網絡搜索后從Fred Grott( http://knol.google.com/k/fred-grott/advance-tabs/ )找到的新答案。
這使您可以設置文本顏色的selector
,以便在選擇或不選擇選項卡時可以使用不同的顏色。 如果您為選項卡使用不同的背景顏色,這可能非常有用。 當然,您也可以只使用普通顏色而不是選擇器。
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
,它可以通過View
對象檢索,可以從TabWidget
對象訪問。
請查看Fred的示例,了解有關變量聲明以及其他技巧的更多信息和上下文。
在自定義主題更改中設置樣式
<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>
Danny 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>
之后,最后在tab類文件中添加Dannyy的答案
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(R.color.text_tab_indicator));
也可以在不使用java的情況下聲明顏色的變化 - 這可能更好。
我對text_tab_indicator進行了更改(注意textColor已更改為'color'):
<?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"
/>
聲明您的text_tab_indicator位於/ res / color中,如您在樣式中所需的顏色
<style name="TabText">
<item name="android:textColor">@color/tab_text_color</item>
</style>
它就像一個魅力(對我來說)。
干杯,蘭德爾
嘗試ColorStateLists 。 祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.