【问题标题】:How do I change text color to a specific textView in styles in xml?如何将文本颜色更改为 xml 样式中的特定 textView?
【发布时间】:2018-02-01 22:19:18
【问题描述】:

在 android studios xml 文件样式中,我想更改特定 textView 的颜色,但不知道如何引用它。我只能找到

<item name="android:textColor">@color/colorRed</item>

这会将颜色更改为 all textViews,但我需要它来定位特定的。通过谷歌搜索没有找到任何东西。似乎是一件简单的事情,只是似乎找不到解决方案......有什么想法吗?它必须在 xml 而不是 java 代码中完成。

更新: 更清楚一点:我有三个样式的 textView,我希望它们都具有不同的颜色。如果我使用上面的代码,所有的 textViews 都会改变。如何为样式中的每个 textView 设置一种颜色?

【问题讨论】:

    标签: android xml layout colors


    【解决方案1】:

    您可以通过设置样式直接在 TextView 的 xml 中执行此操作。它看起来像这样:

    <TextView
      android:id="@+id/text_view_id_one"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="@string/hello_one"
      style="@style/ColorOne"/>
    
    <TextView
      android:id="@+id/text_view_id_two"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="@string/hello_two"
      style="@style/ColorTwo"/>
    
    <TextView
      android:id="@+id/text_view_id_three"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="@string/hello_three"
      style="@style/ColorThree"/>
    

    然后在你的样式文件中你可以创建三个独立的样式:

    <style name="ColorOne" parent="AppTheme">
        <item name="android:textColor">@color/color_one</item>
    </style>
    
    <style name="ColorTwo" parent="AppTheme">
        <item name="android:textColor">@color/color_two</item>
    </style>
    
    <style name="ColorThree" parent="AppTheme">
        <item name="android:textColor">@color/color_three</item>
    </style>
    

    【讨论】:

    • 谢谢,但这无济于事,因为我想根据样式设置主题。单击按钮我想更改样式,因此所有 textView 颜色信息都需要在样式 xml 文件中....
    • 在您的问题中,您说“我想更改特定 textView 的颜色”,这就是上面的代码所做的。对不起,我想我不完全明白你想要完成什么?如果您想更改特定文本视图的颜色,这就是我的方式,如果您希望根据样式更改所有文本视图,那么它就是您在原始帖子中的代码。如果这是您想要的,您可以指定您希望 TextView 基于的样式
    • 好吧,我可能有点太含糊了。我会更新原来的帖子:)
    • 哦,我明白了!更新确实让它更清楚了。我已经更新了我给出的答案。这是否是您正在寻找的更多内容?
    • 谢谢,这很有道理!感谢您的帮助,很抱歉在最初的帖子中不够清楚:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多