当包含文本的样式时,文本的颜色不会更改文本颜色应用于文本的外观文本视图



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

当我在"textAppearance"属性下应用这些样式时,上述样式中文本的颜色都没有改变。它在文本视图的"样式"属性下工作。

//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 文档,我们可以在"textAppearance"属性下应用文本颜色样式。

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

尝试将小部件中的文本颜色设置为 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。导入和链接问题有时可以像这样解决。

这个片段对我有用

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

和文本视图是

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

有时如果你不给样式android:textColor,你可能会遇到文本颜色不会出现在你的TextView中的问题,所以解决方案是只给样式完全<item name="android:textColor">@color/yourColor</item>而不是这个<item name="textColor">@color/yourColor</item>..你的问题会得到解决:)

最新更新