【问题标题】:how to change the color of the tabs indicator text in android?如何在android中更改选项卡指示器文本的颜色?
【发布时间】:2010-05-10 17:01:54
【问题描述】:

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

【问题讨论】:

    标签: android tabs tabwidget


    【解决方案1】:

    这是我在经过一些网络搜索后从 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 文件夹中的 selector xml file

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

    【讨论】:

    • tv 为空,findViewById(android.R.id.title) 没有找到
    【解决方案2】:

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

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

    【讨论】:

    • @DroidBase,@Alex Volovoy,我应该在哪里放置样式。当我将其作为单独的 xml 文件放在 values 文件夹下时,出现错误 Error retrieving parent for item: No resource found that matches the given name 'Widget'.
    • @Mithun:请发布一些代码 sn-p 找到确切的解决方案。我的猜测是你没有提到父属性。即覆盖 xml 对象的默认样式
    • 嘿@Alex,看起来不错,但我无法尝试......这是我第一次尝试风格......你能详细说明你想说什么吗? @DroidBase 你也请解释一下你在 apidemos 评论中想说什么??????
    • @Farhan 根据您使用的父主题,您可以添加类似 。这将使属性找到。
    • 这是一个不错的解决方案,但仅适用于罗伯特在此处设置父级的评论。要设置必要的属性 parent 而不必弄清楚要使用的另一个主题,您可以使用这个parent="@style/AppTheme" 来使用styles.xml 中现有的AppTheme 样式
    【解决方案3】:

    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 在颜色/值文件夹下看起来像这样

    <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 在 text_tab_indicator.xml 文件中应该是 android:color
    • getColorStateList 已弃用,因此请使用 ContextCompat.getColorStateList(context, R.color.text_tab_indicator)。
    • 失败,因为 tv 为空,似乎无法找到 ID 为标题的 TextView。我的代码在包含 TabLayout 的片段的 OnCreate 中
    【解决方案4】:

    颜色的变化也可以不用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"
        />
    

    将位于 /res/color 中的 text_tab_indicator 声明为样式中所需的颜色

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

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

    干杯, 兰德尔

    【讨论】:

    • 虽然这个解决方案看起来很完美,但它在我的两个设备(Android 5 和 2.3)上都不起作用。
    猜你喜欢
    • 1970-01-01
    • 2015-10-24
    • 2016-08-24
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多