如何更改android中选项卡指示符文本的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2804688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:28:36  来源:igfitidea点击:

how to change the color of the tabs indicator text in android?

androidtabstabwidget

提问by Praveen

how to change the color of the text indicator of tab? i can change the icon using selector tag refered the example. but cant to the text color. how?

如何更改选项卡文本指示器的颜色?我可以使用示例中的选择器标签更改图标。但不能到文字颜色。如何?

采纳答案by Alex Volovoy

Style it in your custom theme change

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

<item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item> 

and

<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>     

回答by Danny C

Here is a new answer I found from Fred Grott (http://knol.google.com/k/fred-grott/advance-tabs/) after a little web searching.
This lets you set a selectorfor text color so a different color can be used when tab is selected or not. Which can be very useful if you are using a different background color for the tab if its selected. Of course you can also just throw in a plain color and not a selector.

这是我在网上搜索后从 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));

Where R.color.text_tab_indicator is a selector xml filelocated in your res/drawable folder.

In other words, the indicator text really is a TextViewwhich is retrievable via the Viewobject which can be accessed from the TabWidgetobject.
Take a look at Fred's examples for more info and context regarding the variable declarations as well as other tricks.

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

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

回答by Shahzad Imam

Danny C's answer is 100% correct.I just wanted to add something to it to make a complete answer with resource file.

Danny C 的回答是 100% 正确的。我只是想添加一些东西来用资源文件做出一个完整的答案。

The text_tab_indicator under res/color file

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>

And this text_tab_unselected & text_tab_selected will look like this under colors/values folder

这个 text_tab_unselected & text_tab_selected 在颜色/值文件夹下看起来像这样

<resources> 
<color name="text_tab_selected">#ffffff</color>
<color name="text_tab_unselected">#95ab45</color>

After that finally add Dannyy's answer in tab class file

之后最后在选项卡类文件中添加丹尼的答案

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

回答by javahead76

The change in color can also be stated without using java - which is probably better.

不使用 java 也可以说明颜色的变化 - 这可能更好。

I made changes to the text_tab_indicator (notice textColor was changed to 'color'):

我对 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>

Set the style of the TabWidget to point to a specific style in your xml code:

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

<TabWidget
    ...
    style="@style/TabText"
    />

Declare your text_tab_indicator located in /res/color as you desired color in the style

将位于 /res/color 中的 text_tab_indicator 声明为您想要的样式颜色

<style name="TabText">
    <item name="android:textColor">@color/tab_text_color</item>
</style>

It worked like a charm (for me).

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

Cheers, Randall

干杯,兰德尔

回答by trgraglia

Try ColorStateLists. Good Luck.

试试ColorStateLists。祝你好运。