2017-02-09 123 views
0

我嘗試學習Android的主題,並陷入困境設置TextView TEXTCOLOR到另一種顏色,然後這個全球:如何TextView的文本顏色設置爲特定的主題顏色

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

但我真的需要做這樣嗎?我只是想創建一個顏色屬性不是創建新的風格

這裏是主題,it's兩個主題和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

它應該是'機器人:文字顏色= 「@顏色/ chatBubbleTextColor」' – Pztar

+0

的可能的複製[Android的,如何主題字體顏色只是TextView的小工具?(http://stackoverflow.com/questions/15960029/android-how-to-theme-font-color-for-just-textview-widgets) –

回答

0

我找到了答案,以我自己的問題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> 
0

進入你的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" 
       /> 
+0

所以,我真的必須創建一個新樣式,比如'style name =「chatBubbleTextColor」....'爲什麼不添加到' 「BaseTheme.Bohemia」'? –

+0

@ErikHellberg是的,當然,如果你想使用「BaseTheme.Bhehemia」使用style =「@ style/BaseTheme.Bohemia」。 –

+0

但現在我的紅色主題和波希米亞都有相同的'chatBubbleTextColor'。我真的需要在'BaseTheme.Bohemia'和'BaseTheme.Red'中獲得'chatBubbleTextColor'。我怎麼做?喜歡我的問題說如何設置TextView文本顏色到特定的主題顏色 –

0

,如果你在,如果你把它設置爲任何TextView顏色android:textColor="?attr/chatBubbleTextColor"那麼如果在AppTheme設置chatBubbleTextColor它將工作應用該主題的活動與你在這個主題<style name="BaseTheme.Bohemia" parent="AppTheme">設置顏色爲chatBubbleTextColor風格也將提供給整個應用

相關問題