2017-07-10 45 views
0

我想減少我的xml代碼重複。所以我在textView中爲文本製作了一些標準樣式。我們可以在'style'屬性下應用樣式,並在textView中應用'android:textAppearance'屬性。當包含textColor的樣式應用於文本時,文本的顏色不會改變textView的外觀

下面是一些款式我的文字做appearance-

<style name="Grey"> 
    <item name="android:textColor"> #333333 </item> 
</style> 

<style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
    <item name="android:textColor"> #00FF00 </item> 
    <item name="android:typeface">monospace</item> 
    <item name="android:textSize">20sp</item> 
</style> 

當我申請根據「textAppearance」這些樣式屬性的文本的顏色是不是在沒有上述風格變化。它在textView的'style'屬性下工作。

//textColor not working 
<TextView 
    android:id="@+id/profile_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Full Name" 
    android:textAppearance="@style/CodeFont"/> 

//textColor working 
<TextView 
    android:id="@+id/profile_name" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Full Name" 
    style="@style/CodeFont"/> 

我希望他們在'textAppearance'屬性下工作,以便我可以在'style'屬性下應用其他一些樣式。根據android documentation,我們可以在'textAppearance'屬性下應用textColor樣式。

請提出一些解決方案。 感謝

回答

0

嘗試爲空設置文本的顏色在你的widget是這樣的:

<TextView 
android:id="@+id/profile_name" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="Full Name" 
android:textColor="@null" //add this line 
android:textAppearance="@style/CodeFont"/> 

另外,我覺得你應該嘗試無效緩存和重新啓動的Android工作室。導入和鏈接問題有時可以像這樣解決。

1

這個片段對我的作品

<style name="fonts" parent="TextAppearance.AppCompat"> 
    <item name="android:textColor">#245546</item> 
    <item name="android:textSize">30dp</item> 
</style> 

和TextView的是

<TextView 
    android:id="@+id/sample" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Welcome to stackoverflow" 
    android:textAppearance="@style/fonts"/> 
相關問題