MultipleTheme换肤功能详解

本文详细介绍了Android换肤框架MultipleTheme的实现原理。通过定义主题属性、样式,然后在Activity中设置主题并同步更新控件样式。切换主题时,使用动画效果,并通过遍历视图和接口回调实现动态换肤。尽管该框架有效,但需要大量自定义组件,可定制性和解耦性有待提高。
摘要由CSDN通过智能技术生成

前段时间刚好看到一篇换肤开源框架,MultipleTheme,这边来研究研究到底怎么实现的:
这里写图片描述

Android每个页面都有自己的主题风格,而主题样式可以在Style.xml里面自定义。自然就可以在这里面做文章,并且便于管理。

首先在attrs.xml里面定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="main_bg" format="reference|color"/>
    <attr name="main_textcolor" format="reference|color"/>
    <attr name="second_bg" format="reference|color"/>
    <attr name="second_textcolor" format="reference|color"/>
</resources>

然后在style.xml里面设置相应的属性值:

  • 这里分为theme1, theme2。分别对应不同的主题风格;
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="theme_1" >
        <item name="main_bg">@color/bg_main_normal</item>
        <item name="main_textcolor">@color/textcolor_main_normal</item>
        <item name="second_bg">@color/bg_second_normal</item>
        <item name="second_textcolor">@color/textcolor_second_normal</item>
    </style>

    <style name="theme_2">
        <item name="main_bg">@color/bg_main_dark</item>
        <item name="main_textcolor">@color/textcolor_main_dark</item>
        <item name="second_bg">@color/bg_second_dark</item>
        <item name="second_textcolor">@color/textcolor_second_dark</item>
    </style>
</resources>

相应的颜色值color.xml中去定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bg_main_normal">#ffffff</color>
    <color name="textcolor_main_normal">#ff0000</color>
    <color name="bg_main_dark">#000000</color>
    <color name="textcolor_main_dark">#ffffff</color>
    <color name="bg_second_normal">#0000ff</color>
    <color name="textcolor_second_normal">#00ff00</color>
    <color name="bg_second_dark">#ffffff</color>
    <color name="textcolor_second_dark">#000000</color>
</resources>
重点是怎么去用主题,具体讲解如下:

所有的Activity都继承BaseActivity,在oncreate()创建Activity实例时,会设置该Activity的Theme(主题),然后布局文件各元素会自定获取Style.xml定义好的属性进行展示;

package derson.com.multipletheme;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;

import derson.com.multipletheme.colorUi.util.SharedPreferencesMgr;

/**
 * Created by chengli on 15/6/14.
 */
public class BaseActivity extends Activity{
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(SharedPreferencesMgr.getInt("theme", 0) == 1) {
            setTheme(R.style.theme_2);
        } else {
            setTheme(R.style.theme_1);
        }
    }
}

相应的布局文件

<derson.com.multipletheme.colorUi.widget.ColorRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/main_bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <derson.com.multipletheme.colorUi.widget.ColorTextView
        android:textColor="?attr/main_textcolor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <derson.com.multipletheme.colorUi.widget.ColorButton
        android:id="@+id/btn"
        android:text="换肤"
        android:layout_centerInParent="true"
        android:textColor="?attr/main_textcolor"
        android:layout_width="100dip"
        android:layout_height="80dip" />

    <derson.com.multipletheme.colorUi.widget.ColorButton
        android:id="@+id/btn_2"
        android:layout_centerHorizontal="true"
        android:text="下一页"
        android:layout_below="@id/btn"
        android:layout_marginTop="30dip"
        android:textColor="?attr/main_textcolor"
        android:layout_width="100dip"
        android:layout_height="80dip" /&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值