我尝试学习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>
答案 0 :(得分:1)
我找到了自己问题的答案here
基本上它是这样的:
In the file attr.xml I define this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="ChatBubbleBackGroundColor" format="reference|color" />
<attr name="ChatBubbleTextColor" format="reference|color" />
</resources>
Next I add to my two 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>
and finally in my layout
<TextView
android:id="@+id/quoteTitle"
android:textColor="?ChatBubbleTextColor"
android:BackGround="?ChatBubbleBackGroundColor"
...
</TextView>
答案 1 :(得分:0)
使用style="@style/chatBubbleTextColor"
而不是android:textColor="?attr/chatBubbleTextColor"
进入TextView。
像这样的东西
<TextView
style="@style/chatBubbleTextColor"
android:id="@+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
答案 2 :(得分:0)
如果您将此主题应用于其中的任何活动,如果您将其设置为任何chatBubbleTextColor
颜色<style name="BaseTheme.Bohemia" parent="AppTheme">
,则您已在此主题TextView
中为android:textColor="?attr/chatBubbleTextColor"
设置了颜色如果在chatBubbleTextColor
样式中设置AppTheme
,它将适用于整个应用