2017-02-09 65 views
0

我在应用程序中有几个主题,它工作正常。现在,我想成立一​​个聊天气泡的文字颜色为红色,当用户选择BaseTheme.Red和文本颜色为橙色,当用户选择BaseTheme.Orange(见下面的代码)当多个主题可用时,如何通过Android主题设置独特的TextView文本颜色

It's只是我想聊天泡泡文字的权利像'红'和橙色为橙色主题和应用程序中的所有其他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" 

但是我不能让它工作,也许它不工作那样?

如何使用以下主题进行此项工作?

下面是两个主题RedOrange

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

我你为什么改变整个主题,而不是改变文本的一个问题颜色由这样的东西? ''color name =“errorColor”>#f00 textView.setTextColor(getResources()。getColor(R.color.errorColor));' –

+0

当你像你说的那样设置颜色'textView.setTextColor(getResources ().getColor(R.color.errorC olor));'这意味着所有主题的文本颜色都是相同的。我不明白你说什么:“我有一个问题,你为什么要改变整个主题” –

+0

你打算将多个主题同时设置为一个textview吗?当你为一个特定的textview设置颜色文本时,它是否适用于其他视图? –

回答

0

我找到了答案,以我自己的问题here

Basically it goes like this: 

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> 
相关问题