android如何切换皮肤

1、先定义attr文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="layout_bg" format="reference"/>
    <attr name="layout_item_bg" format="reference"/>
    <attr name="textColor" format="reference"/>
</resources>

2、定义实际的属性值文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme.Default">
        <item name="layout_bg">@color/left_draw_bg</item>
        <item name="layout_item_bg">@color/window_background</item>
        <item name="textColor">@color/gray</item>
    </style>

    <style name="AppTheme.Green">
        <item name="layout_bg">@color/left_draw_bg</item>
        <item name="layout_item_bg">@color/window_background</item>
        <item name="textColor">@color/green</item>
    </style>

    <style name="AppTheme.Red">
        <item name="layout_bg">@color/left_draw_bg</item>
        <item name="layout_item_bg">@color/window_background</item>
        <item name="textColor">@color/red</item>
    </style>

</resources>

3、使用我们自定义的属性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/layout_bg"
    android:orientation="vertical">
<TextView
                android:id="@+id/tv_left_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/s_117"
                android:layout_marginTop="@dimen/s_117"
                android:textColor="?attr/textColor"
                android:text="@string/app_name" />

通过?attr/+自定义属性名引用

4、Activity调用相应的主题

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_Green);
        setContentView(R.layout.activity_main);
        initView();
    }

可见android系统,让人先简单的使用主题,只要调用setTheme就可以搞定。

系统将AppTheme.Green改成了AppTheme_Green来调用。

原文地址:https://www.cnblogs.com/jiduoduo/p/5238514.html