问题
I want to reduce my xml code repetition. So I made some standard styles for text in textView. We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.
Below are some styles I made for text appearance-
<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>
When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles. It's working under 'style' attribute of 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"/>
I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute. And according to android documentation we can apply textColor styles under 'textAppearance' attribute.
Please suggest some solution to this. Thanks
回答1:
Try setting the text color in your widget as null like this:
<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"/>
Also, I think you should try to Invalidate cache and Restart Android Studio. Import and linking issues can be solved like this sometimes.
回答2:
This snippet works for me
<style name="fonts" parent="TextAppearance.AppCompat">
<item name="android:textColor">#245546</item>
<item name="android:textSize">30dp</item>
</style>
and textview is
<TextView
android:id="@+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to stackoverflow"
android:textAppearance="@style/fonts"/>
来源:https://stackoverflow.com/questions/45007858/color-of-the-text-is-not-changing-when-style-including-textcolor-is-applied-to-t