并非所有视图都有不同的主题样式

Different theme styles for not all views

在我的 Android 应用程序中,我有两个不同的主题(浅色和深色)。 例如:

<style name="AppThemeDark" parent="Theme.AppCompat">
        <item name="colorPrimary">@android:color/black</item>
        <item name="colorPrimaryDark">@android:color/black</item>
        <item name="colorAccent">@android:color/holo_red_dark</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="windowActionModeOverlay">true</item>
</style>

<style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionModeOverlay">true</item>
</style>

所以,现在我可以向 TextView 应用不同的文本颜色(白色代表深色主题,黑色代表浅色):

<item name="android:textViewStyle">@style/TextViewDark</item>

<style name="TextViewDark">
        <item name="android:textColor">?android:attr/colorAccent</item>
</style>

但它将应用于所有 TextView。

主要问题,是否可以在 XML 中(不是以编程方式)下一个:

浅色主题:TextViews 文本颜色的一半是黑色,另一半是绿色。 黑色主题:在 Light 主题中为黑色的 TextView - 红色,另一半 - 蓝色(在 Light 主题中为绿色)。

您已在 styles.xml

中定义
<style name="TextViewDark">
    <item name="android:textColor">?android:attr/colorAccent</item>
</style>

<style name="TextViewLight">
    <item name="android:textColor">@color/green</item>
</style>

然后你可以在main.xml

中使用它
<LinearLayout>

    <TextView
        android:id="@+id/light_text_view"
        android:text"i´m use light theme"
        style="@style/TextViewLight"/>

    <TextView
        android:id="@+id/dark_text_view"
        android:text"i´m use darktheme"
        style="@style/TextViewDark"/>

</LinearLayout>

您不需要在 AppThemes

中定义样式

创建 2 classes 扩展 TextView

public class OneTextView extends TextView {

    public OneTextView(Context context) {
        super(context);
        init(context);
    }

    public OneTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public OneTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        int[] attrs = new int[] { R.attr.myFirstColor}; 
        TypedArray ta = context.obtainStyledAttributes(attrs); 
        int appColor = ta.getColor(0, 0); 
        ta.recycle();

        // set theme color
        setTextColor(appColor);
    }
}

public class SecondTextView extends TextView {

    public SecondTextView(Context context) {
        super(context);
        init(context);
    }

    public SecondTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        int[] attrs = new int[] { R.attr.mySecondColor}; 
        TypedArray ta = context.obtainStyledAttributes(attrs); 
        int appColor = ta.getColor(0, 0); 
        ta.recycle();

        // set theme color
        setTextColor(appColor);
    }
}

每个 class 你都可以像这样在 xml 中使用

<com.route.to.class.OneTextView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

OneTextView 可以有黑色和红色

SecondTextView 可以有绿色和蓝色

values

中定义attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myFirstColor" format="color" />
    <attr name="mySecondColor" format="color" />
</resources>

然后在您的 styles.xml 中为每个主题定义颜色:

<style name="Theme.MyApp" parent="@style/Theme.Light">
   <item name="myFirstColor">@color/black</item>
   <item name="mySecondColor">@color/green</item>
</style>

<style name="Theme.MyApp.Dark" parent="@style/Theme.Dark">
   <item name="myFirstColor">@color/green</item>
   <item name="mySecondColor">@color/blue</item>
</style>