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

Vip*_*mar 6 android text appearance textcolor android-styles

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

以下是我为文本外观制作的一些样式-

<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>
Run Code Online (Sandbox Code Playgroud)

当我在 '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"/>
Run Code Online (Sandbox Code Playgroud)

我希望它们在“textAppearance”属性下工作,以便我可以在“style”属性下应用一些其他样式。根据android 文档,我们可以在 'textAppearance' 属性下应用 textColor 样式。

请对此提出一些解决方案。谢谢

Yas*_*han 5

尝试将小部件中的文本颜色设置为 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"/>
Run Code Online (Sandbox Code Playgroud)

另外,我认为您应该尝试使缓存无效并重新启动 Android Studio。有时可以像这样解决导入和链接问题。