我想减少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的'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"/>
我希望它们在“textAppearance”属性下工作,这样我就可以在“style”属性下应用一些其他样式。根据android documentation,我们可以在'textAppearance‘属性下应用textColor样式。
请对此提出一些解决方案。谢谢
发布于 2017-07-10 17:14:59
尝试将小部件中的文本颜色设置为空,如下所示:
<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。导入和链接问题有时可以像这样解决。
发布于 2017-07-10 17:20:15
此代码片段适用于我
<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"/>
发布于 2020-12-15 16:15:29
有时如果你不给android:textColor
in样式,你可能会遇到这个问题,文本颜色不会出现在你的TextView中,所以解决方案是完全给出<item name="android:textColor">@color/yourColor</item>
in样式,而不是这个<item name="textColor">@color/yourColor</item>
。您的问题将得到解决:)
https://stackoverflow.com/questions/45007858
复制相似问题