【问题标题】:How to set TextView text color to specific Theme color如何将 TextView 文本颜色设置为特定主题颜色
【发布时间】:2017-02-09 14:51:01
【问题描述】:

我尝试学习 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 对两者都不同

波西米亚应用主题和红色应用主题

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

【问题讨论】:

标签: android android-theme textcolor


【解决方案1】:

我找到了我自己的问题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>

【讨论】:

    【解决方案2】:

    在您的 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"
                     />
    

    【讨论】:

    • 那么我真的要创建一个像style name="chatBubbleTextColor".... 这样的新样式为什么不添加到"BaseTheme.Bohemia"
    • @ErikHellberg 是的,当然,如果你想使用“BaseTheme.Bohemia”,请使用 style="@style/BaseTheme.Bohemia"。
    • 但是现在我的红色主题和波西米亚有相同的chatBubbleTextColor。我真的需要在 BaseTheme.BohemiaBaseTheme.Red 中获取 chatBubbleTextColor 。我怎么做?就像我的问题说如何将 TextView 文本颜色设置为特定的主题颜色
    • @ErikHellberg 进入你的主题添加 你的颜色
    【解决方案3】:

    你已经在这个主题&lt;style name="BaseTheme.Bohemia" parent="AppTheme"&gt; 中为chatBubbleTextColor 设置了颜色,如果你将此主题应用于任何活动,如果你将它设置为任何TextView 颜色android:textColor="?attr/chatBubbleTextColor",那么如果设置chatBubbleTextColor,它将起作用在AppTheme 样式中,它将可用于整个应用程序

    【讨论】:

      猜你喜欢
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多