当包含textColor的样式应用于文本时,文本的颜色不会改变textView的外观

问题描述:

我想减少我的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样式。

请提出一些解决方案。 感谢

尝试为空设置文本的颜色在你的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工作室。导入和链接问题有时可以像这样解决。

这个片段对我的作品

<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 *" 
    android:textAppearance="@style/fonts"/>