[英]Again on Android theme: use a specific text color for some 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 和 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>
我在這里找到了我自己的問題的答案。
基本上是這樣的:
在文件attr.xml
我定義了這個:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="ChatBubbleBackGroundColor" format="reference|color" />
<attr name="ChatBubbleTextColor" format="reference|color" />
</resources>
接下來我添加到我的兩個BaseTheme
s:
<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>
在您的 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"
/>
如果您將此主題應用於任何活動,則您已在此主題中為chatBubbleTextColor
設置顏色 android: TextView
= <style name="BaseTheme.Bohemia" parent="AppTheme">
android:textColor="?attr/chatBubbleTextColor"
那么如果在AppTheme
樣式中設置chatBubbleTextColor
它將工作,它將對整個應用程序可用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.