简体   繁体   English

当包含 textColor 的样式应用于 textView 的 textAppearance 时,文本的颜色没有改变

[英]Color of the text is not changing when style including textColor is applied to textAppearance of textView

I want to reduce my xml code repetition.我想减少我的 xml 代码重复。 So I made some standard styles for text in textView.所以我为textView中的文本做了一些标准样式。 We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.我们可以在 'style' 属性下应用样式,也可以在 textView 中应用 'android:textAppearance' 属性。

Below are some styles I made for text 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>

When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles.当我在“textAppearance”属性下应用这些样式时,文本的颜色在上述任何样式中都没有改变。 It's working under 'style' attribute of textView.它在 textView 的“样式”属性下工作。

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

I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute.我希望它们在“textAppearance”属性下工作,以便我可以在“style”属性下应用一些其他样式。 And according to android documentation we can apply textColor styles under 'textAppearance' attribute.根据android 文档,我们可以在“textAppearance”属性下应用 textColor 样式。

Please suggest some solution to this.请对此提出一些解决方案。 Thanks谢谢

Try setting the text color in your widget as null like this:尝试将小部件中的文本颜色设置为空,如下所示:

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

Also, I think you should try to Invalidate cache and Restart Android Studio.另外,我认为您应该尝试使缓存无效并重新启动 Android Studio。 Import and linking issues can be solved like this sometimes.有时可以这样解决导入和链接问题。

This snippet works for me这个片段对我有用

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

and textview is文本视图是

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

Sometimes if you don't give android:textColor in styles you can have this issue that text color won't appear in your TextView , So the solution is to just give completely <item name="android:textColor">@color/yourColor</item> in styles rather than this <item name="textColor">@color/yourColor</item> .. your problem would be resolved:)有时,如果您不在样式中提供android:textColor ,您可能会遇到文本颜色不会出现在TextView中的问题,因此解决方案是完全提供<item name="android:textColor">@color/yourColor</item>样式而不是这个<item name="textColor">@color/yourColor</item> .. 你的问题会得到解决:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM