当textColor的样式应用于textView的textAppearance时,文本的颜色不会改变

时间:2017-07-10 09:03:56

标签: android text android-styles textcolor appearance

我想减少我的xml代码重复。所以我在textView中为文本制作了一些标准样式。我们可以在' style'属性以及' android:textAppearance' textView中的属性。

以下是我为文字外观制作的一些风格 -

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

当我将这些样式应用于&#39; textAppearance&#39;属性文本的颜色在以上任何一种样式中都没有变化。它是在&#39; style&#39; 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"/>

我希望他们能够在&text;&#39; textAppearance&#39;属性,以便我可以在&#39; style&#39;下应用其他一些风格。属性。根据{{​​3}},我们可以在&#39; textAppearance&#39;下应用textColor样式。属性。

请为此建议一些解决方案。 感谢

3 个答案:

答案 0 :(得分:3)

尝试将窗口小部件中的文本颜色设置为null,如下所示:

<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 Studio。有时可以像这样解决导入和链接问题。

答案 1 :(得分:2)

这个代码段对我有用

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

答案 2 :(得分:0)

有时候,如果您不给样式android:textColor,您可能会遇到这样的问题,即文本颜色不会出现在 TextView 中,因此解决方案是完全给{{ 1}}而不是<item name="android:textColor">@color/yourColor</item>样式。.您的问题将得到解决:)

相关问题