styles.xml 中的单个 TextView 背景颜色主题
Theme a single TextView background color in styles.xml
我有一个有多个主题的应用程序。我有一个 TextView,其背景需要根据每个主题更改颜色,所有其他 TextView 保留其默认主题。我创建了一个自定义 TextView 小部件并将其设置为我的 xml 布局文件中的 TextView。
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);
}
}
布局
<*My Package*.CustomHeaderTextView
android:layout_width="250dp"
android:layout_height="40dp" />
如何访问自定义 TextView 并更改 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>
我找到了一种可以为特定 TextView 设置不同背景颜色的方法。此外,您还可以根据自己的每个主题进行设置。
解决方案:
在 attr.xml
中创建您自己的属性自定义属性
下面是实现:
第 1 步
首先,在您的 res/values 文件夹中创建一个 attr.xml 文件并插入以下内容:
res/values/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="customTextViewBackAttributeColor" format="color" />
</resources>
第 2 步
您创建的那个属性应该在您拥有的每个主题中设置颜色,如下所示:
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>
第 3 步
最后,将该属性设置为自定义视图的背景色。
备注
您可以将此颜色设置为特定 TextView 的背景。这样,只有那个 TextView 会有不同的背景颜色(而不是每个主题中定义的默认背景颜色)。这样,您无需创建 CustomView 即可拥有不同的背景颜色。
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"/>
我有一个有多个主题的应用程序。我有一个 TextView,其背景需要根据每个主题更改颜色,所有其他 TextView 保留其默认主题。我创建了一个自定义 TextView 小部件并将其设置为我的 xml 布局文件中的 TextView。
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);
}
}
布局
<*My Package*.CustomHeaderTextView
android:layout_width="250dp"
android:layout_height="40dp" />
如何访问自定义 TextView 并更改 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>
我找到了一种可以为特定 TextView 设置不同背景颜色的方法。此外,您还可以根据自己的每个主题进行设置。
解决方案:
在 attr.xml
中创建您自己的属性自定义属性下面是实现:
第 1 步
首先,在您的 res/values 文件夹中创建一个 attr.xml 文件并插入以下内容:
res/values/attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="customTextViewBackAttributeColor" format="color" />
</resources>
第 2 步
您创建的那个属性应该在您拥有的每个主题中设置颜色,如下所示:
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>
第 3 步
最后,将该属性设置为自定义视图的背景色。
备注
您可以将此颜色设置为特定 TextView 的背景。这样,只有那个 TextView 会有不同的背景颜色(而不是每个主题中定义的默认背景颜色)。这样,您无需创建 CustomView 即可拥有不同的背景颜色。
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"/>