2017-02-09 2 views
0

J'essaie d'apprendre Android Thèmes et a eu des ennuis mettre un TextView TextColor à une autre couleur alors ce monde:Comment définir la couleur du texte TextView couleur spécifique Thème

<item name="android:textColor">@color/white</item> 

J'ai créé ceci:

<item name="chatBubbleTextColor">@color/material_bohemia_500</item> 

et pensé que je pouvais l'utiliser dans le xml TextView comme

android:textColor="?attr/chatBubbleTextColor" 

mais je ne peux pas i t travailler peut-être que ça ne marche pas comme ça?
Je sais que je peux le faire comme ceci:

<style name="BohemiachatBubbleTextColor" parent="android:Theme"> 
    <item name="android:textColor">@color/material_bohemia_500</item> 
</style> 

Mais dois-je vraiment faire comme ça? Je veux seulement créer un attribut couleur ne pas créer un nouveau style

Voici le thème, ESt deux thèmes et chatBubbleTextColor est différent pour les deux

Bohême App Thème et App Rouge Thème

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

il devrait être 'android: textColor = "couleur @/chatBubbleTextColor"' – Pztar

+0

double possible de [Android, Comment le thème de la couleur de la police pour seulement TextView Widgets?] (Http://stackoverflow.com/questions/15960029/android-how-to-theme-font-color-for-just-textview-widgets) –

Répondre

0

J'ai trouvé la réponse à ma propre question here

Fondamentalement, il va comme ceci:

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

dans votre TextView utilisation style="@style/chatBubbleTextColor" au lieu de android:textColor="?attr/chatBubbleTextColor". Quelque chose comme ça

<TextView 
       style="@style/chatBubbleTextColor" 
       android:id="@+id/my_id" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       /> 
+0

Donc je dois vraiment créer un nouveau style comme 'style name =" chatBubbleTextColor "....' 'pourquoi ne pas ajouter à' "BaseTheme.Bohemia" '? –

+0

@ErikHellberg oui, bien sûr, si vous voulez utiliser "BaseTheme.Bohemia", utilisez style = "@ style/BaseTheme.Bohemia". –

+0

Mais maintenant, mon thème rouge et Bohême ont tous les deux le même 'chatBubbleTextColor'. J'ai vraiment besoin d'obtenir 'chatBubbleTextColor' à l'intérieur de' BaseTheme.Bohemia' et 'BaseTheme.Red'. Comment je fais ça? Comme ma question dire comment définir la couleur du texte TextView à la couleur du thème spécifique –

0

Vous avez défini la couleur pour chatBubbleTextColor dans ce thème <style name="BaseTheme.Bohemia" parent="AppTheme"> si vous appliquez ce thème à toute activité avec en ce que si vous le réglez sur une TextView couleur android:textColor="?attr/chatBubbleTextColor" il fonctionnera si elle est définie chatBubbleTextColor dans AppTheme le style, il sera disponible pour l'application entière