2017-02-09 28 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>