当多个主题可用时,如何通过Android主题设置唯一的TextView文本颜色

当多个主题可用时,如何通过Android主题设置唯一的TextView文本颜色,android,android-theme,Android,Android Theme,我有一个应用程序中的几个主题,它可以正常工作。现在我想在用户选择BaseTheme.red时将聊天泡泡文本颜色设置为红色,在用户选择BaseTheme.orange时将文本颜色设置为橙色(请参见下面的代码) 只有聊天泡泡文本正确,我希望“红色”为红色,橙色为橙色主题,应用程序中的所有其他文本视图文本颜色将具有默认主题颜色 我试图学习Android主题,但在将此聊天文本视图文本颜色设置为另一种颜色时遇到了麻烦,然后是此全局: <item name="android:textColor">

我有一个应用程序中的几个主题,它可以正常工作。现在我想在用户选择
BaseTheme.red
时将聊天泡泡文本颜色设置为红色,在用户选择
BaseTheme.orange
时将文本颜色设置为橙色(请参见下面的代码)

只有聊天泡泡文本正确,我希望“红色”为红色,橙色为橙色主题,应用程序中的所有其他文本视图文本颜色将具有默认主题颜色

我试图学习Android主题,但在将此聊天文本视图文本颜色设置为另一种颜色时遇到了麻烦,然后是此全局:

<item name="android:textColor">@color/white</item>
我想我可以在TextView xml中使用它,比如

android:textColor="?attr/chatBubbleTextColor"
但是我不能让它工作也许它不是那样工作的

我如何才能与下面的主题使这项工作

这里有两个主题
红色
橙色

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



    <!-- Orange App Theme -->
    <style name="BaseTheme.Orange" parent="AppTheme">
        <!-- Attributes for all APIs -->
        <item name="colorPrimary">@color/material_orange_500</item>
        <item name="colorPrimaryDark">@color/material_orange_700</item>
        <item name="colorAccent">@color/material_orange_a700</item>
        <item name="dialogTheme">@style/AppTheme.Dialog.Orange</item>
        <item name="alertDialogTheme">@style/AppTheme.Dialog.Alert.Orange</item>
        <item name="android:windowBackground">@color/material_orange_300</item>
    </style>

    <style name="AppTheme.Orange" parent="BaseTheme.Orange">
        <!-- API specific attributes 14+ -->
        <item name="selectableRectDrawableColored">@drawable/state_list_selectable_rect_orange</item>
        <item name="selectableRoundedRectDrawableColored">@drawable/state_list_selectable_rounded_rect_orange</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 attribute not working-->
        <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>

假的
真的
@样式/AppTheme.Dialog
@style/AppTheme.Dialog.Alert
@颜色/选择器\黑色\按下
@样式/应用首选项主题
@style/Widget.AppCompat.EditText
@可绘制/状态列表可选择黑色
@可绘制/状态列表可选择白色
@可绘制/状态列表可选择黑色
@可绘制/状态列表可选圆形黑色
@可绘制/状态列表可选圆形白色
@可绘制/状态列表可选圆形黑色
@颜色/材质\u橙色\u 500
@颜色/材质\u橙色\u 700
@颜色/材质\u橙色\u a700
@style/AppTheme.Dialog.Orange
@样式/AppTheme.Dialog.Alert.Orange
@颜色/材质\u橙色\u 300
@可绘制/状态列表可选择橙色
@可绘制/状态列表可选圆形橙色
@颜色/材料\u红色\u 500
@颜色/材质\u红色\u 700
@颜色/材质\u红色\u a700
@样式/AppTheme.Dialog.Red
@样式/AppTheme.Dialog.Alert.Red
@颜色/材质\u红色\u 300
@颜色/材料\u红色\u 500
@可绘制/状态列表可选择红色
@可绘制/状态列表可选择四舍五入红色

我找到了自己问题的答案

基本上是这样的:

在文件attr.xml中,我定义如下:

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

接下来,我将添加到我的两个基本主题:

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

@颜色/材料\u红色\u a200
@颜色/材质\u红色\u a700
@颜色/材质\u橙色\u a200
@颜色/材质\u橙色\u a700
最后在我的布局中

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

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

基本上是这样的:

在文件attr.xml中,我定义如下:

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

接下来,我将添加到我的两个基本主题:

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

@颜色/材料\u红色\u a200
@颜色/材质\u红色\u a700
@颜色/材质\u橙色\u a200
@颜色/材质\u橙色\u a700
最后在我的布局中

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

我有一个问题,为什么要改变整个主题,而不是像这样改变文本颜色<代码>#f00 textView.setTextColor(getResources().getColor(R.color.errorColor))当您喜欢时,您可以这样说设置颜色
textView.setTextColor(getResources().getColor(R.color.error‌​颜色)这意味着所有主题的文本颜色都相同。我不明白你是怎么说的:“我有一个问题,为什么你要更改整个主题”你要同时将多个主题设置为一个文本视图吗?当你为一个特定的文本视图设置颜色文本时,它是否适用于其他视图?我的建议是,你可以研究一下这一点,了解我的问题是关于什么的。你不可以为你的聊天泡泡定义一种样式,然后在那里设置文本颜色吗?例如,通过使用样式,您可以将此布局XML:
转换为:
我有一个问题,为什么您要更改整个主题,而不是通过这样的方式更改文本颜色<代码>#f00 textView.setTextColor(getResources().getColor(R.color.errorColor))当您喜欢时,您可以这样说设置颜色
textView.setTextColor(getResources().getColor(R.color.error‌​颜色)这意味着所有主题的文本颜色都相同。我不明白你是怎么说的:“我有一个问题,为什么你要更改整个主题”你要同时将多个主题设置为一个文本视图吗?当你为一个特定的文本视图设置颜色文本时,它是否适用于其他视图?我的建议是,你可以研究一下这一点,了解我的问题是关于什么的。你不可以为你的聊天泡泡定义一种样式,然后在那里设置文本颜色吗?例如,通过使用样式,可以将此布局XML:
转换为: