前段时间刚好看到一篇换肤开源框架,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