[英]How to set unique TextView text color through Android Theme when multiple Themes are available
我在应用程序中有几个主题,它可以正常工作。 现在,我想将用户选择BaseTheme.Red
时的聊天气泡文本颜色设置为红色,而当用户选择BaseTheme.Orange
时将文本颜色设置为橙色(请参见下面的代码)
我只希望聊天气泡文本正确,如“红色”代表红色,橙色主题代表橙色,应用程序中的所有其他TextView文本颜色将具有默认的主题颜色 。
我尝试学习Android主题,并遇到了麻烦,将此聊天TextView文本颜色设置为另一种颜色,然后将此设置为全局:
<item name="android:textColor">@color/white</item>
我创建了这个:在BaseTheme.Red
内部
<item name="chatBubbleTextColor">@color/material_red_500</item>
并且以为我可以在TextView xml中使用它,例如
android:textColor="?attr/chatBubbleTextColor"
但我无法使其正常工作,也许它不能那样工作?
我如何使用以下主题进行这项工作?
这是Red
和Orange
两个主题:
<!-- 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>
我在这里找到了自己的问题的答案
基本上,它是这样的:
在文件attr.xml中,我定义了以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="ChatBubbleBackGroundColor" format="reference|color" />
<attr name="ChatBubbleTextColor" format="reference|color" />
</resources>
接下来,我添加到我的两个BaseTheme中:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.