如何更改android中标签指示文字的颜色?

Pra*_*een 24 tabs android tabwidget

如何更改选项卡文本指示器的颜色?我可以使用选择标签更改图标参考示例.但不能文字颜色.怎么样?

Dan*_*y C 30

以下是我在一次网络搜索后从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));
Run Code Online (Sandbox Code Playgroud)

其中R.color.text_tab_indicator是位于res/drawable文件夹中的选择器xml文件.

换句话说,指示符文本实际上是可以通过可从对象访问的对象TextView检索ViewTabWidget.
请查看Fred的示例,了解有关变量声明以及其他技巧的更多信息和上下文.


Ale*_*voy 24

在自定义主题更改中设置样式

<item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item> 
Run Code Online (Sandbox Code Playgroud)

<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>     
Run Code Online (Sandbox Code Playgroud)

  • @DroidBase,@ Alex Volovoy,我应该在哪里放置样式.当我把它作为一个单独的xml文件放在values文件夹下时,得到错误`错误检索项目的父项:找不到匹配给定名称'Widget'的资源. (3认同)

Sha*_*mam 20

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>
Run Code Online (Sandbox Code Playgroud)

此text_tab_unselected&text_tab_selected在colors/values文件夹下将如下所示

<resources> 
<color name="text_tab_selected">#ffffff</color>
<color name="text_tab_unselected">#95ab45</color>
Run Code Online (Sandbox Code Playgroud)

之后,最后在tab类文件中添加Dannyy的答案

final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);        
tv.setTextColor(this.getResources().getColorStateList(R.color.text_tab_indicator));
Run Code Online (Sandbox Code Playgroud)

  • `android:textColor`应该是text_tab_indicator.xml文件中的`android:color`. (3认同)
  • 不推荐使用getColorStateList,因此请使用ContextCompat.getColorStateList(context,R.color.text_tab_indicator). (2认同)

jav*_*d76 6

也可以在不使用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>
Run Code Online (Sandbox Code Playgroud)

将TabWidget的样式设置为指向xml代码中的特定样式:

<TabWidget
    ...
    style="@style/TabText"
    />
Run Code Online (Sandbox Code Playgroud)

声明您的text_tab_indicator位于/ res/color中,如您在样式中所需的颜色

<style name="TabText">
    <item name="android:textColor">@color/tab_text_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力(对我来说).

干杯,兰德尔