Android简单的换肤demo

demo链接在文章的最后。

1.首先准备两套皮肤。在res/values/styles中定义两套颜色风格。我设置的是一套白天模式,一套夜间模式。

<style name="DayTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="bg">#ffffff</item>
    <item name="text_color">#000000</item>
    <item name="btn_color">#00ff00</item>
</style>
<style name="NightTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="bg">#000000</item>
    <item name="text_color">#ffffff</item>
    <item name="btn_color">#ff0000</item>
</style>

2.在res/values/下新建一个文件attrs,内容如下,可以注意到name值和style中的name值是相同的。
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="bg" format="reference|color"/>
    <attr name="text_color" format="reference|color"/>
    <attr name="btn_color" format="reference|color"/>
</resources>
3.在布局中引用颜色值,如: android :background= "?attr/bg",这样就能在白天模式和夜间模式的两套风格就设置好了。接下来,就是在代码中切换颜色了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?attr/bg"
    tools:context="com.towatt.charge.towatt.testskin2.MainActivity">

    <TextView
        android:id="@+id/tv_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击跳转下一个界面"
        android:textColor="?attr/text_color"
        />

    <Button
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:text="换肤"
        android:onClick="click"
        android:textColor="?attr/text_color"
        android:background="?attr/btn_color"
        />

    <EditText
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:textColor="?attr/text_color"
        android:textColorHint="?attr/text_color"
        android:hint="EditText" />
</LinearLayout>
5.设置一个基础的BaseActivity,所有activity都继承这个BaseActivity。
public class BaseActivity extends AppCompatActivity{
    public static int theme = -1; //当前的主题
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //如果主题不是默认值, 设置指定主题
        if (theme != -1) {
            setTheme(theme);
        }
    }
}
6.新建一个activity继承BaseActivity,设置换肤,并重新加载,就可以开到换肤效果了。
public void click(View v) {
    if(BaseActivity.theme==-1||BaseActivity.theme==R.style.DayTheme) {
        BaseActivity.theme=R.style.NightTheme;
    }else{
        BaseActivity.theme=R.style.DayTheme;
    }
    reload();
}

/**
 * 重新启动界面
 */
private void reload() {
    //关闭当前Activity
    finish();
    overridePendingTransition(0, 0);
    //启动Activity
    Intent intent = getIntent();
    startActivity(intent);
}

需要注意的是,换肤后,所有的界面都要重新startActivity,执行了BaseActivity中的onCreate方法之后才能看到换肤效果,如果只是finish,看到上一个界面,是不能看到换肤效果的。

demo链接:  http://download.csdn.net/detail/hrobbie/9550200

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值