如何将 TextView 文本颜色设置为特定的主题颜色

How to set TextView text color to specific Theme color

我尝试学习 Android 主题,但在将 TextView TextColor 设置为另一种颜色时遇到了麻烦,然后是这个全局颜色:

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

我创建了这个:

 <item name="chatBubbleTextColor">@color/material_bohemia_500</item>

并认为我可以在 TextView xml 中使用它,例如

android:textColor="?attr/chatBubbleTextColor"

但我无法让它工作,也许它不能那样工作?
我知道我可以这样做:

<style name="BohemiachatBubbleTextColor" parent="android:Theme">
    <item name="android:textColor">@color/material_bohemia_500</item>
</style>

但我真的必须这样做吗?我只想创建一个颜色属性而不是创建一个新样式

这是主题,它是两个主题,chatBubbleTextColor 两者不同

Bohemia App Theme and Red App Theme

<!-- Base Theme -->
<style name="BaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Attributes for all APIs -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="dialogTheme">@style/AppTheme.Dialog</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert</item>
    <item name="colorControlHighlight">@color/selector_black_pressed</item>
    <!-- Theme for the Preferences -->
    <item name="preferenceTheme">@style/AppPreferenceTheme</item>
    <!-- Theme for the pacv_placesAutoCompleteTextV -->
    <item name="pacv_placesAutoCompleteTextViewStyle">@style/Widget.AppCompat.EditText</item>



<!-- Default App Theme -->
<style name="AppTheme" parent="BaseTheme">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawable">@drawable/state_list_selectable_rect_black</item>
    <item name="selectableRectDrawableInverse">@drawable/state_list_selectable_rect_white</item>
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_black</item>
    <item name="selectableRoundedRectDrawable">@drawable/state_list_selectable_rounded_rect_black</item>
    <item name="selectableRoundedRectDrawableInverse">@drawable/state_list_selectable_rounded_rect_white</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_black</item>
</style>



<!-- Bohemia App Theme -->
<style name="BaseTheme.Bohemia" parent="AppTheme">
    <!-- Attributes for all APIs -->
    <item name="colorPrimary">@color/material_bohemia_400</item>
    <item name="colorPrimaryDark">@color/material_bohemia_500</item>
    <item name="colorAccent">@color/material_bohemia_a100</item>
    <item name="dialogTheme">@style/AppTheme.Dialog.Bohemia</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Bohemia</item>
    <item name="android:windowBackground">@color/material_bohemia_600</item>
    <!-- Sets the color of the control when it is not activated like an unchecked checkbox. -->
    <item name="colorControlNormal">@color/material_bohemia_a200</item>
    <!-- Chat bubble -->
    <item name="chatBubbleTextColor">@color/material_bohemia_500</item>

</style>

<style name="AppTheme.Bohemia" parent="BaseTheme.Bohemia">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_bohemia</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_bohemia</item>
    <!-- Add your custom overall styles here -->
</style>

<!-- Red App Theme -->
<style name="BaseTheme.Red" parent="AppTheme">
    <!-- Attributes for all APIs -->
    <item name="colorPrimary">@color/material_red_500</item>
    <item name="colorPrimaryDark">@color/material_red_700</item>
    <item name="colorAccent">@color/material_red_a700</item>
    <item name="dialogTheme">@style/AppTheme.Dialog.Red</item>
    <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Red</item>
    <item name="android:windowBackground">@color/material_red_300</item>
    <!-- Chat bubble -->
    <item name="chatBubbleTextColor">@color/material_red_500</item>
</style>

<style name="AppTheme.Red" parent="BaseTheme.Red">
    <!-- API specific attributes 14+ -->
    <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_red</item>
    <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_red</item>

    <!-- Add your custom overall styles here -->
</style>

在您的 TextView 中使用 style="@style/chatBubbleTextColor" 而不是 android:textColor="?attr/chatBubbleTextColor"。 像这样

<TextView
                style="@style/chatBubbleTextColor"
                android:id="@+id/my_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 />

如果您将此主题应用于任何 activity,则您已在该主题中为 chatBubbleTextColor 设置颜色 <style name="BaseTheme.Bohemia" parent="AppTheme"> 如果您将其设置为任何 TextView 颜色android:textColor="?attr/chatBubbleTextColor" 如果在 AppTheme 样式中设置 chatBubbleTextColor 它将工作,它将对整个应用程序可用

我找到了我自己的问题的答案 here

基本上是这样的:

在文件attr.xml中我这样定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="ChatBubbleBackGroundColor" format="reference|color" />
    <attr name="ChatBubbleTextColor" format="reference|color" />
</resources>

接下来我添加到我的两个 BaseThemes:

<style name="BaseTheme.Red" parent="AppTheme">
   <item name="ChatBubbleBackGroundColor">@color/material_red_a200</item>
   <item name="ChatBubbleTextColor">@color/material_red_a700</item>
</style>

<style name="BaseTheme.Orange" parent="AppTheme">
   <item name="ChatBubbleBackGroundColor">@color/material_orange_a200</item>
   <item name="ChatBubbleTextColor">@color/material_orange_a700</item>
</style>

最后在我的布局中:

<TextView
    android:id="@+id/quoteTitle"
    android:textColor="?ChatBubbleTextColor"
    android:BackGround="?ChatBubbleBackGroundColor"
    ...
</TextView>