在styles.xml 中为单个TextView 背景色设置主题 [英] Theme a single TextView background color in styles.xml

查看:27
本文介绍了在styles.xml 中为单个TextView 背景色设置主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个主题的应用程序.我有一个 TextView,它的背景需要随每个主题更改颜色,所有其他 TextView 保持默认主题.我创建了一个自定义 TextView 小部件并将其设置为我的 xml 布局文件中的 TextView.

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);
    }
}

布局

    <*My Package*.CustomHeaderTextView
        android:layout_width="250dp"
        android:layout_height="40dp" />

如何访问自定义 TextView 并更改我的 style.xml 中每个主题的背景颜色?

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>

推荐答案

我找到了一种方法,可以为特定的 TextView 设置不同的背景颜色.此外,您还可以根据您拥有的每个主题进行设置.

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.

在 attr.xml 中创建自己的属性自定义属性

Creating your own attribute custom attribute in attr.xml

以下是实现:

首先,在您的 res/values 文件夹中创建一个 attr.xml 文件并插入以下内容:

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>

步骤 2

您创建的该属性应该在您拥有的每个主题中设置一个颜色,如下所示:

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>

步骤 3

最后,将该属性设置为自定义视图的背景颜色.

Step 3

Finally, set that attribute as the background color of your custom view.

注意

您可以将此颜色设置为特定 TextView 的背景.这样,只有那个 TextView 会有不同的背景颜色(而不是每个主题中定义的默认背景颜色).这样,您就不需要创建一个 CustomView 只是为了拥有不同的背景颜色.

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"/>

这篇关于在styles.xml 中为单个TextView 背景色设置主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆