转自:http://blog.csdn.net/bboyfeiyu/article/details/48322023

Colorful是基于Theme,无需重启Activity、无需自定义View,方便的实现日间、夜间模式,github地址为 https://github.com/bboyfeiyu/Colorful。

Colorful 动态换肤开源库

基于Theme的Android动态换肤开源库,以简单的方式实现夜间模式。

效果如下:

一、使用方式

1.1 自定义属性

<?xml version="1.0" encoding="utf-8"?><resources>        <attr name="root_view_bg" format="reference|color" />    <attr name="btn_bg" format="reference|color" />    <attr name="text_color" format="reference|color" />resources>
  • 1

1.2 在布局中使用自定义属性设置View的背景、文本颜色等属性

activity_main.xml中的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/root_view"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="?attr/root_view_bg"    tools:context="com.example.androidthemedemo.MainActivity" >    <TextView        android:id="@+id/textview"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="@string/change_theme"        android:textColor="?attr/text_color"        android:textSize="20sp" />    <Button        android:id="@+id/change_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/textview"        android:layout_marginTop="20dp"        android:text="@string/change_theme"        android:textColor="?attr/text_color" />    <Button        android:id="@+id/second_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/change_btn"        android:layout_marginTop="20dp"        android:text="@string/sec_act"         />    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/second_btn"        android:layout_marginTop="20dp" />RelativeLayout>
  • 1

例如上述布局中我们将root_view的背景设置为"?attr/root_view_bg",代表它的背景是自定义属性root_view_bg的值,还有Textview和Button的textColor属性设置为"?attr/text_color"

1.3 定义多个Theme

然后在不同的Theme中为这些属性设置不同的值,例如,通常我们有日间和夜间模式两种颜色模式。styles.xml中的完整代码如下:

<resources>    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">    style>        <style name="AppTheme" parent="AppBaseTheme">    style>        <style name="DayTheme" parent="AppTheme">        <item name="root_view_bg">@drawable/bg_day        "btn_bg">@color/white_btn_color        "text_color">@color/black_tx_color    style>        <style name="NightTheme" parent="AppTheme">        <item name="root_view_bg">@drawable/bg_night        "btn_bg">@color/black_btn_color        "text_color">@color/white_tx_color    style>resources>
  • 1

两个主题下为同一个属性设置了不同的值,达到切换主题时修改View的相关属性的目的。例如定义在colors.xml中的颜色值。

<?xml version="1.0" encoding="utf-8"?><resources>        <color name="white_btn_color">#3BB32Ecolor>    <color name="black_tx_color">#333333color>        <color name="black_btn_color">#aa7788color>    <color name="white_tx_color">#f0f0f0color>resources>
  • 1

1.4 设置要修改的View的属性

下面我们为activity_main.xml中的视图进行换肤设置:

ListView  mNewsListView = (ListView) findViewById(R.id.listview);// 为ListView设置要修改的属性,在这里没有对ListView本身的属性做修改ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView, 0);// 绑定ListView的Item View中的news_title视图,在换肤时修改它的text_color属性listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);// 构建Colorful对象Colorful mColorful = new Colorful.Builder(this)        .backgroundDrawable(R.id.root_view, R.attr.root_view_bg) // 设置view的背景图片        .backgroundColor(R.id.change_btn, R.attr.btn_bg) // 设置按钮的背景色        .textColor(R.id.textview, R.attr.text_color) // 设置文本颜色        .setter(listViewSetter)           // 手动设置setter        .create(); 
  • 1

首先我们定义了一个listViewSetter,该Setter用于为ListView的每个Item View中的news_title控件设置文本颜色,文本颜色的值是自定义属性text_color的颜色值。然后构建Colorful对象,并且id分别为change_btn、root_view、textview的控件绑定特定属性值,例如backgroundDrawable(R.id.root_view, R.attr.root_view_bg)代表root_view的背景Drawable为自定义属性root_view_bg的值,textColor(R.id.textview, R.attr.text_color)表示id为textview的TextView控件的文本颜色为R.attr.text_color的值。这些属性都在不同的Theme中有不同的值,因此切换Theme时就会发生变化。然后我们将listViewSetter添加到Colorful对象中,在修改主题时被遍历ListView中的所有Item View,然后修改news_title控件的文本颜色。

1.5 切换主题

最后通过Colorful对象设置主题即可实现切换,代码如下:

boolean isNight = false ;// 切换主题private void changeThemeWithColorful() {    if (!isNight) {        mColorful.setTheme(R.style.DayTheme);    } else {        mColorful.setTheme(R.style.NightTheme);    }    isNight = !isNight;}
  • 1

时间仓促,功能简单,希望有时间的同学积极加入,将功能慢慢完善起来!

 

更多相关文章

  1. Android(安卓)USB调试开关设置
  2. android NDK 之C调java属性及方法
  3. android 模拟器启动报错:Failed to open the HAX device!
  4. 今天更新SDK到11
  5. 显示界面的时候直接获取到EditText焦点弹出键盘
  6. Windows SublimeText内使用NDK编译Android(安卓)so(不用cygwin)
  7. Android(安卓)横竖屏操作
  8. 密码输入框
  9. Android(安卓)属性动画 实现view翻转 旋转 平移 拉伸 透明度 背

随机推荐

  1. Android中如何判断是否联网
  2. 【Android-File】Android文件的读写
  3. Android(安卓)pthread mutex 实现分析
  4. android 渐变背景
  5. Android(安卓)Error:AAPT: error: resourc
  6. android中的Touch触摸事件传递机制
  7. 【Android】事件分发机制源码解析
  8. android之Display.getRotation()_传感器
  9. android 防止反编译
  10. Android应用程序键盘(Keyboard)消息处理机