【发布时间】:2017-12-13 22:34:44
【问题描述】:
我想减少我的 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”属性下应用这些样式时,文本的颜色在上述样式中都没有改变。它在 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"/>
我希望它们在 'textAppearance' 属性下工作,以便我可以在 'style' 属性下应用一些其他样式。根据android documentation,我们可以在'textAppearance'属性下应用textColor样式。
请为此提出一些解决方案。 谢谢
【问题讨论】:
-
textColor 很可能被默认的 TextView 样式覆盖,因为样式优先于 textAppearance。要检查这一点,您可以在 android:textAppearance="@style/CodeFont" 行之前设置 android:textColor="@null"。
标签: android text android-styles textcolor appearance