问题
I have an application that has multiple themes. I have a single TextView who's background needs to change color with each theme, all other TextView's stay their default theme. I created a custom TextView widget and set it to the TextView in my xml layout file.
public class CustomHeaderTextView extends TextView {
public CustomHeaderTextView(Context context) {
super(context);
}
public CustomHeaderTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomHeaderTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
Layout
<*My Package*.CustomHeaderTextView
android:layout_width="250dp"
android:layout_height="40dp" />
How do I access the custom TextView and change the background color within each of my themes in my styles.xml?
<style name="AppTheme.Blue" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/primaryColor_blue</item>
<item name="colorPrimaryDark">@color/primaryColorDark_blue</item>
<item name="colorAccent">@color/primaryAccent_blue</item>
// Set here
<item name="CustomHeaderTextView:backgroundColor">@color/primaryColorDark_blue</item>
</style>
<style name="AppTheme.Red" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primaryColor_red</item>
<item name="colorPrimaryDark">@color/primaryColorDark_red</item>
<item name="colorAccent">@color/primaryAccent_red</item>
// Set here
<item name="CustomHeaderTextView:backgroundColor">@color/primaryColorDark_red</item>
</style>
回答1:
I found a way where you can set a different background color for a specific TextView. Also, you will be able to set it according to each theme that you have.
Solution:
Creating your own attribute custom attribute in attr.xml
Below is the implementation:
Step 1
First, create a attr.xml file at your res/values folder and insert following content:
res/values/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="customTextViewBackAttributeColor" format="color" />
</resources>
Step 2
That attribute that you created should be set with a Color in every theme that you have as below:
styles.xml
<resources>
<style name="AppTheme.Blue" parent="Theme.AppCompat.NoActionBar">
<!-- Specific Text View Color -->
<item name="customTextViewBackAttributeColor">@color/color_for_theme_blue</item>
</style>
<style name="AppTheme.Red" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Specific Text View Color -->
<item name="customTextViewBackAttributeColor">@color/color_for_theme_red</item>
</style>
</resources>
Step 3
Finally, set that attribute as the background color of your custom view.
Note
You can set this color as background of your specific TextView. This way, only that TextView will have a different background color (and not the default background color defined in each theme). This way, you don't need to create a CustomView only to have a different Background Color.
res/layout/activity_layout.xml
<com.pivoto.gui.generic.CustomHeaderTextView
android:layout_width="250dp"
android:layout_height="40dp"
android:text="Hello World!"
android:background="?customTextViewBackAttributeColor"/>
<TextView
android:layout_width="250dp"
android:layout_height="40dp"
android:text="Hello World2!"
android:background="?customTextViewBackAttributeColor"/>
<TextView
android:layout_width="250dp"
android:layout_height="40dp"
android:text="Hello World3!"
android:background="?customTextViewBackAttributeColor"/>
来源:https://stackoverflow.com/questions/35235703/theme-a-single-textview-background-color-in-styles-xml