├── .gitignore
├── .settings
└── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── ColorfulDemo
├── .settings
│ └── org.eclipse.jdt.core.prefs
├── AndroidManifest.xml
├── ic_launcher-web.png
├── proguard-project.txt
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── drawable-xhdpi
│ │ ├── bg_day.png
│ │ ├── bg_night.png
│ │ └── ic_launcher.png
│ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ ├── layout
│ │ ├── activity_main.xml
│ │ ├── activity_recycler.xml
│ │ └── news_lv_item.xml
│ ├── values-v11
│ │ └── styles.xml
│ ├── values-v14
│ │ └── styles.xml
│ ├── values-w820dp
│ │ └── dimens.xml
│ └── values
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
└── src
│ └── com
│ └── example
│ └── androidthemedemo
│ ├── MainActivity.java
│ └── RecyclerActivity.java
├── LICENSE
├── README.md
├── images
└── colorful.gif
├── libs
├── android-support-v4.jar
└── android-support-v7-recyclerview.jar
├── proguard-project.txt
├── project.properties
└── src
└── com
└── simple
└── colorful
├── Colorful.java
└── setter
├── TextColorSetter.java
├── ViewBackgroundColorSetter.java
├── ViewBackgroundDrawableSetter.java
├── ViewGroupSetter.java
└── ViewSetter.java
/.gitignore:
--------------------------------------------------------------------------------
1 | **/bin/
2 | **/gen/
3 | *.apk
4 | **/.classpath
5 | **/.project
6 | *.class
7 | *.DS_Store
8 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ColorfulDemo/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/ColorfulDemo/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ColorfulDemo/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/ic_launcher-web.png
--------------------------------------------------------------------------------
/ColorfulDemo/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/ColorfulDemo/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-22
15 | android.library.reference.1=..
16 | android.library.reference.2=../../BreakingNews/appcompat_v7
17 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-xhdpi/bg_day.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-xhdpi/bg_day.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-xhdpi/bg_night.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-xhdpi/bg_night.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/ColorfulDemo/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/ColorfulDemo/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
17 |
18 |
26 |
27 |
35 |
36 |
42 |
43 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/layout/activity_recycler.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
20 |
21 |
27 |
28 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/layout/news_lv_item.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
13 |
14 |
24 |
25 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | #3BB32E
6 | #333333
7 |
8 |
9 | #aa7788
10 | #f0f0f0
11 |
12 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | AndroidThemeDemo
5 | 修改主题啦
6 | RecyclerView页面
7 | Settings
8 |
9 |
--------------------------------------------------------------------------------
/ColorfulDemo/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
26 |
27 |
28 |
33 |
34 |
--------------------------------------------------------------------------------
/ColorfulDemo/src/com/example/androidthemedemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.androidthemedemo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import android.app.Activity;
7 | import android.content.Intent;
8 | import android.os.Bundle;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.View.OnClickListener;
12 | import android.view.ViewGroup;
13 | import android.widget.BaseAdapter;
14 | import android.widget.ListView;
15 | import android.widget.TextView;
16 |
17 | import com.simple.colorful.Colorful;
18 | import com.simple.colorful.setter.ViewGroupSetter;
19 |
20 | /**
21 | * 演示换肤的用法,含义按钮、TextView、ListView
22 | *
23 | * @author mrsimple
24 | *
25 | */
26 | public class MainActivity extends Activity {
27 |
28 | ListView mNewsListView;
29 | List mNewsList = new ArrayList();
30 | Colorful mColorful;
31 | boolean isNight = false;
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 | setContentView(R.layout.activity_main);
37 |
38 | // 换肤事件
39 | findViewById(R.id.change_btn).setOnClickListener(new OnClickListener() {
40 |
41 | @Override
42 | public void onClick(View v) {
43 | changeThemeWithColorful();
44 | }
45 | });
46 |
47 | // 第二个Activity,显示的是RecyclerVoew
48 | findViewById(R.id.second_btn).setOnClickListener(new OnClickListener() {
49 |
50 | @Override
51 | public void onClick(View v) {
52 | Intent intent = new Intent(MainActivity.this,
53 | RecyclerActivity.class);
54 | startActivity(intent);
55 | }
56 | });
57 |
58 | mNewsListView = (ListView) findViewById(R.id.listview);
59 | // 模拟数据
60 | mockNews();
61 | mNewsListView.setAdapter(new NewsAdapter());
62 |
63 | // 初始化Colorful
64 | setupColorful();
65 | }
66 |
67 | /**
68 | * 设置各个视图与颜色属性的关联
69 | */
70 | private void setupColorful() {
71 | ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView);
72 | // 绑定ListView的Item View中的news_title视图,在换肤时修改它的text_color属性
73 | listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);
74 |
75 | // 构建Colorful对象来绑定View与属性的对象关系
76 | mColorful = new Colorful.Builder(this)
77 | .backgroundDrawable(R.id.root_view, R.attr.root_view_bg)
78 | // 设置view的背景图片
79 | .backgroundColor(R.id.change_btn, R.attr.btn_bg)
80 | // 设置背景色
81 | .textColor(R.id.textview, R.attr.text_color)
82 | .setter(listViewSetter) // 手动设置setter
83 | .create(); // 设置文本颜色
84 | }
85 |
86 | /**
87 | * 切换主题
88 | */
89 | private void changeThemeWithColorful() {
90 | if (!isNight) {
91 | mColorful.setTheme(R.style.NightTheme);
92 | } else {
93 | mColorful.setTheme(R.style.DayTheme);
94 | }
95 | isNight = !isNight;
96 | }
97 |
98 | private void mockNews() {
99 | for (int i = 0; i < 20; i++) {
100 | mNewsList.add("News Title - " + i);
101 | }
102 | }
103 |
104 | /**
105 | *
106 | * @author mrsimple
107 | *
108 | */
109 | class NewsAdapter extends BaseAdapter {
110 |
111 | @Override
112 | public int getCount() {
113 | return mNewsList.size();
114 | }
115 |
116 | @Override
117 | public String getItem(int position) {
118 | return mNewsList.get(position);
119 | }
120 |
121 | @Override
122 | public long getItemId(int position) {
123 | return position;
124 | }
125 |
126 | @Override
127 | public View getView(int position, View convertView, ViewGroup parent) {
128 | NewsViewHolder viewHolder = null;
129 | if (convertView == null) {
130 | convertView = LayoutInflater.from(parent.getContext()).inflate(
131 | R.layout.news_lv_item, parent, false);
132 | viewHolder = new NewsViewHolder();
133 | viewHolder.newsTitleView = (TextView) convertView
134 | .findViewById(R.id.news_title);
135 |
136 | convertView.setTag(viewHolder);
137 | } else {
138 | viewHolder = (NewsViewHolder) convertView.getTag();
139 | }
140 |
141 | viewHolder.newsTitleView.setText(getItem(position));
142 | return convertView;
143 | }
144 |
145 | }
146 |
147 | public static class NewsViewHolder {
148 | public TextView newsTitleView;
149 |
150 | }
151 |
152 | }
153 |
--------------------------------------------------------------------------------
/ColorfulDemo/src/com/example/androidthemedemo/RecyclerActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.androidthemedemo;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import android.app.Activity;
7 | import android.os.Bundle;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.support.v7.widget.RecyclerView.Adapter;
11 | import android.support.v7.widget.RecyclerView.ViewHolder;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.view.View.OnClickListener;
15 | import android.view.ViewGroup;
16 | import android.widget.TextView;
17 |
18 | import com.simple.colorful.Colorful;
19 | import com.simple.colorful.setter.ViewGroupSetter;
20 |
21 | /**
22 | * 演示RecyclerView的换肤效果
23 | * @author mrsimple
24 | *
25 | */
26 | public class RecyclerActivity extends Activity {
27 |
28 | RecyclerView mRecyclerView;
29 | List mNewsList = new ArrayList();
30 | Colorful mColorful;
31 | boolean isNight = false;
32 |
33 | @Override
34 | protected void onCreate(Bundle savedInstanceState) {
35 | super.onCreate(savedInstanceState);
36 |
37 | setTheme(R.style.NightTheme);
38 |
39 | setContentView(R.layout.activity_recycler);
40 |
41 | findViewById(R.id.change_btn).setOnClickListener(new OnClickListener() {
42 |
43 | @Override
44 | public void onClick(View v) {
45 | changeThemeWithColorful();
46 | }
47 | });
48 |
49 | mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
50 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
51 | mRecyclerView.setItemViewCacheSize(0);
52 | // 模拟数据
53 | mockNews();
54 | mRecyclerView.setAdapter(new NewsAdapter());
55 |
56 | // 初始化Colorful
57 | setupColorful();
58 | }
59 |
60 | /**
61 | * 设置各个视图与颜色属性的关联
62 | */
63 | private void setupColorful() {
64 | ViewGroupSetter recyclerViewSetter = new ViewGroupSetter(mRecyclerView,
65 | 0);
66 | recyclerViewSetter.childViewTextColor(R.id.news_title,
67 | R.attr.text_color);
68 |
69 | mColorful = new Colorful.Builder(this)
70 | .backgroundColor(R.id.change_btn, R.attr.btn_bg) // 设置背景色
71 | .setter(recyclerViewSetter) // 手动设置setter
72 | .create(); // 设置文本颜色
73 | }
74 |
75 | private void changeThemeWithColorful() {
76 | if (!isNight) {
77 | mColorful.setTheme(R.style.DayTheme);
78 | } else {
79 | mColorful.setTheme(R.style.NightTheme);
80 | }
81 | isNight = !isNight;
82 | }
83 |
84 | private void mockNews() {
85 | for (int i = 0; i < 20; i++) {
86 | mNewsList.add("News Title - " + i);
87 | }
88 | }
89 |
90 | /**
91 | *
92 | * @author mrsimple
93 | *
94 | */
95 | class NewsAdapter extends Adapter {
96 |
97 | @Override
98 | public int getItemCount() {
99 | return mNewsList.size();
100 | }
101 |
102 | public String getItem(int position) {
103 | return mNewsList.get(position);
104 | }
105 |
106 | @Override
107 | public long getItemId(int position) {
108 | return position;
109 | }
110 |
111 | @Override
112 | public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
113 | View itemView = LayoutInflater.from(RecyclerActivity.this).inflate(
114 | R.layout.news_lv_item, parent, false);
115 | return new NewsViewHolder(itemView);
116 | }
117 |
118 | @Override
119 | public void onBindViewHolder(NewsViewHolder viewHolder, int position) {
120 | viewHolder.newsTitleView.setText(getItem(position));
121 | }
122 | }
123 |
124 | public static class NewsViewHolder extends ViewHolder {
125 |
126 | public TextView newsTitleView;
127 |
128 | public NewsViewHolder(View itemView) {
129 | super(itemView);
130 |
131 | newsTitleView = (TextView) itemView.findViewById(R.id.news_title);
132 | }
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Mr.Simple
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](http://www.android-gems.com/lib/bboyfeiyu/Colorful)
2 |
3 | # Colorful 动态换肤开源库
4 |
5 | 基于Theme的Android动态换肤开源库,以简单的方式实现夜间模式。
6 |
7 | * 【该方案可以用于切换ui样式,但是还不完善,有需要的同学可自行修改框架扩展自己需要的功能】
8 |
9 | 效果如下:
10 |
11 | 
12 |
13 |
14 | `Demo工程依赖support v7,请自行添加依赖`
15 |
16 | ## 一、使用方式
17 |
18 | ### 1.1 自定义属性
19 |
20 | ```xml
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | ```
31 |
32 | ### 1.2 在布局中使用自定义属性设置View的背景、文本颜色等属性
33 |
34 | activity_main.xml中的布局:
35 |
36 |
37 | ```xml
38 |
45 |
46 |
54 |
55 |
63 |
64 |
72 |
73 |
79 |
80 |
81 |
82 | ```
83 |
84 | 例如上述布局中我们将root_view的背景设置为`"?attr/root_view_bg"`,代表它的背景是自定义属性root_view_bg的值,还有Textview和Button的textColor属性设置为`"?attr/text_color"`。
85 |
86 | ### 1.3 定义多个Theme
87 |
88 | 然后在不同的Theme中为这些属性设置不同的值,例如,通常我们有日间和夜间模式两种颜色模式。styles.xml中的完整代码如下:
89 |
90 | ```xml
91 |
92 |
93 |
95 |
96 |
97 |
99 |
100 |
101 |
106 |
107 |
108 |
113 |
114 |
115 |
116 | ```
117 |
118 | 两个主题下为同一个属性设置了不同的值,达到切换主题时修改View的相关属性的目的。例如定义在colors.xml中的颜色值。
119 |
120 | ```xml
121 |
122 |
123 |
124 |
125 | #3BB32E
126 | #333333
127 |
128 |
129 | #aa7788
130 | #f0f0f0
131 |
132 |
133 | ```
134 |
135 | ### 1.4 设置要修改的View的属性
136 |
137 | 下面我们为activity_main.xml中的视图进行换肤设置:
138 |
139 |
140 | ```java
141 | ListView mNewsListView = (ListView) findViewById(R.id.listview);
142 |
143 | // 为ListView设置要修改的属性,在这里没有对ListView本身的属性做修改
144 | ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView, 0);
145 | // 绑定ListView的Item View中的news_title视图,在换肤时修改它的text_color属性
146 | listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);
147 |
148 |
149 | // 构建Colorful对象
150 | Colorful mColorful = new Colorful.Builder(this)
151 | .backgroundDrawable(R.id.root_view, R.attr.root_view_bg) // 设置view的背景图片
152 | .backgroundColor(R.id.change_btn, R.attr.btn_bg) // 设置按钮的背景色
153 | .textColor(R.id.textview, R.attr.text_color) // 设置文本颜色
154 | .setter(listViewSetter) // 手动设置setter
155 | .create();
156 |
157 | ```
158 |
159 |
160 | 首先我们定义了一个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控件的文本颜色。
161 |
162 | ### 1.5 切换主题
163 | 最后通过Colorful对象设置主题即可实现切换,代码如下:
164 |
165 | ```java
166 |
167 | boolean isNight = false ;
168 |
169 | // 切换主题
170 | private void changeThemeWithColorful() {
171 | if (!isNight) {
172 | mColorful.setTheme(R.style.DayTheme);
173 | } else {
174 | mColorful.setTheme(R.style.NightTheme);
175 | }
176 | isNight = !isNight;
177 | }
178 | ```
179 |
--------------------------------------------------------------------------------
/images/colorful.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/images/colorful.gif
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/libs/android-support-v7-recyclerview.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hehonghui/Colorful/0be237e87b69b083e47af70d36333b08b387791b/libs/android-support-v7-recyclerview.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-22
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/Colorful.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | import android.app.Activity;
7 | import android.content.res.Resources.Theme;
8 | import android.support.v4.app.Fragment;
9 | import android.view.View;
10 | import android.widget.TextView;
11 |
12 | import com.simple.colorful.setter.TextColorSetter;
13 | import com.simple.colorful.setter.ViewBackgroundColorSetter;
14 | import com.simple.colorful.setter.ViewBackgroundDrawableSetter;
15 | import com.simple.colorful.setter.ViewSetter;
16 |
17 | /**
18 | * 主题切换控制类
19 | *
20 | * @author mrsimple
21 | *
22 | */
23 | public final class Colorful {
24 | /**
25 | * Colorful Builder
26 | */
27 | Builder mBuilder;
28 |
29 | /**
30 | * private constructor
31 | *
32 | * @param builder
33 | */
34 | private Colorful(Builder builder) {
35 | mBuilder = builder;
36 | }
37 |
38 | /**
39 | * 设置新的主题
40 | *
41 | * @param newTheme
42 | */
43 | public void setTheme(int newTheme) {
44 | mBuilder.setTheme(newTheme);
45 | }
46 |
47 | /**
48 | *
49 | * 构建Colorful的Builder对象
50 | *
51 | * @author mrsimple
52 | *
53 | */
54 | public static class Builder {
55 | /**
56 | * 存储了视图和属性资源id的关系表
57 | */
58 | Set mElements = new HashSet();
59 | /**
60 | * 目标Activity
61 | */
62 | Activity mActivity;
63 |
64 | /**
65 | * @param activity
66 | */
67 | public Builder(Activity activity) {
68 | mActivity = activity;
69 | }
70 |
71 | /**
72 | *
73 | * @param fragment
74 | */
75 | public Builder(Fragment fragment) {
76 | mActivity = fragment.getActivity();
77 | }
78 |
79 | private View findViewById(int viewId) {
80 | return mActivity.findViewById(viewId);
81 | }
82 |
83 | /**
84 | * 将View id与存储该view背景色的属性进行绑定
85 | *
86 | * @param viewId
87 | * 控件id
88 | * @param colorId
89 | * 颜色属性id
90 | * @return
91 | */
92 | public Builder backgroundColor(int viewId, int colorId) {
93 | mElements.add(new ViewBackgroundColorSetter(findViewById(viewId),
94 | colorId));
95 | return this;
96 | }
97 |
98 | /**
99 | * 将View id与存储该view背景Drawable的属性进行绑定
100 | *
101 | * @param viewId
102 | * 控件id
103 | * @param colorId
104 | * Drawable属性id
105 | * @return
106 | */
107 | public Builder backgroundDrawable(int viewId, int drawableId) {
108 | mElements.add(new ViewBackgroundDrawableSetter(
109 | findViewById(viewId), drawableId));
110 | return this;
111 | }
112 |
113 | /**
114 | * 将TextView id与存储该TextView文本颜色的属性进行绑定
115 | *
116 | * @param viewId
117 | * TextView或者TextView子类控件的id
118 | * @param colorId
119 | * 颜色属性id
120 | * @return
121 | */
122 | public Builder textColor(int viewId, int colorId) {
123 | TextView textView = (TextView) findViewById(viewId);
124 | mElements.add(new TextColorSetter(textView, colorId));
125 | return this;
126 | }
127 |
128 | /**
129 | * 用户手动构造并且添加Setter
130 | *
131 | * @param setter
132 | * 用户自定义的Setter
133 | * @return
134 | */
135 | public Builder setter(ViewSetter setter) {
136 | mElements.add(setter);
137 | return this;
138 | }
139 |
140 | /**
141 | * 设置新的主题
142 | *
143 | * @param newTheme
144 | */
145 | protected void setTheme(int newTheme) {
146 | mActivity.setTheme(newTheme);
147 | makeChange(newTheme);
148 | }
149 |
150 | /**
151 | * 修改各个视图绑定的属性
152 | */
153 | private void makeChange(int themeId) {
154 | Theme curTheme = mActivity.getTheme();
155 | for (ViewSetter setter : mElements) {
156 | setter.setValue(curTheme, themeId);
157 | }
158 | }
159 |
160 | /**
161 | * 创建Colorful对象
162 | *
163 | * @return
164 | */
165 | public Colorful create() {
166 | return new Colorful(this);
167 | }
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/setter/TextColorSetter.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful.setter;
2 |
3 | import android.content.res.Resources.Theme;
4 | import android.widget.TextView;
5 | /**
6 | * TextView 文本颜色Setter
7 | * @author mrsimple
8 | *
9 | */
10 | public class TextColorSetter extends ViewSetter {
11 |
12 | public TextColorSetter(TextView textView, int resId) {
13 | super(textView, resId);
14 | }
15 |
16 | public TextColorSetter(int viewId, int resId) {
17 | super(viewId, resId);
18 | }
19 |
20 | @Override
21 | public void setValue(Theme newTheme, int themeId) {
22 | if (mView == null) {
23 | return;
24 | }
25 | ((TextView) mView).setTextColor(getColor(newTheme));
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/setter/ViewBackgroundColorSetter.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful.setter;
2 |
3 | import android.content.res.Resources.Theme;
4 | import android.view.View;
5 |
6 | /**
7 | * View的背景色Setter
8 | * @author mrsimple
9 | *
10 | */
11 | public class ViewBackgroundColorSetter extends ViewSetter {
12 |
13 | public ViewBackgroundColorSetter(View target, int resId) {
14 | super(target, resId);
15 | }
16 |
17 | public ViewBackgroundColorSetter(int viewId, int resId) {
18 | super(viewId, resId);
19 | }
20 |
21 | @Override
22 | public void setValue(Theme newTheme, int themeId) {
23 | if ( mView != null ) {
24 | mView.setBackgroundColor(getColor(newTheme));
25 | }
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/setter/ViewBackgroundDrawableSetter.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful.setter;
2 |
3 | import android.content.res.Resources.Theme;
4 | import android.content.res.TypedArray;
5 | import android.graphics.drawable.Drawable;
6 | import android.view.View;
7 |
8 | /**
9 | * View的背景Drawabler Setter
10 | * @author mrsimple
11 | *
12 | */
13 | public final class ViewBackgroundDrawableSetter extends ViewSetter {
14 |
15 | public ViewBackgroundDrawableSetter(View targetView, int resId) {
16 | super(targetView, resId);
17 | }
18 |
19 |
20 | public ViewBackgroundDrawableSetter(int viewId, int resId) {
21 | super(viewId, resId);
22 | }
23 |
24 | @SuppressWarnings("deprecation")
25 | @Override
26 | public void setValue(Theme newTheme, int themeId) {
27 | if ( mView == null ) {
28 | return ;
29 | }
30 | TypedArray a = newTheme.obtainStyledAttributes(themeId,
31 | new int[] { mAttrResId });
32 | int attributeResourceId = a.getResourceId(0, 0);
33 | Drawable drawable = mView.getResources().getDrawable(
34 | attributeResourceId);
35 | a.recycle();
36 | mView.setBackgroundDrawable(drawable);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/setter/ViewGroupSetter.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful.setter;
2 |
3 | import java.lang.reflect.Field;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 | import java.util.HashSet;
7 | import java.util.Set;
8 |
9 | import android.content.res.Resources.Theme;
10 | import android.support.v7.widget.RecyclerView;
11 | import android.util.Log;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.AbsListView;
15 |
16 | /**
17 | * ViewGroup类型的Setter,用于修改ListView、RecyclerView等ViewGroup类型的Item
18 | * View,核心思想为遍历每个Item View中的子控件,然后根据用户绑定的view
19 | * id与属性来将View修改为当前Theme下的最新属性值,达到ViewGroup控件的换肤效果。
20 | *
21 | * TODO : Color与Drawable的设计问题,是否需要修改为桥接模式 {@see ViewBackgroundColorSetter}、
22 | * {@see ViewBackgroundDrawableSetter}
23 | *
24 | * @author mrsimple
25 | *
26 | */
27 | public class ViewGroupSetter extends ViewSetter {
28 |
29 | /**
30 | * ListView的子试图的Setter
31 | */
32 | protected Set mItemViewSetters = new HashSet();
33 |
34 | /**
35 | *
36 | * @param targetView
37 | * @param resId
38 | */
39 | public ViewGroupSetter(ViewGroup targetView, int resId) {
40 | super(targetView, resId);
41 | }
42 |
43 | public ViewGroupSetter(ViewGroup targetView) {
44 | super(targetView, 0);
45 | }
46 |
47 | /**
48 | * 设置View的背景色
49 | *
50 | * @param viewId
51 | * @param colorId
52 | * @return
53 | */
54 | public ViewGroupSetter childViewBgColor(int viewId, int colorId) {
55 | mItemViewSetters.add(new ViewBackgroundColorSetter(viewId, colorId));
56 | return this;
57 | }
58 |
59 | /**
60 | * 设置View的drawable背景
61 | *
62 | * @param viewId
63 | * @param drawableId
64 | * @return
65 | */
66 | public ViewGroupSetter childViewBgDrawable(int viewId, int drawableId) {
67 | mItemViewSetters.add(new ViewBackgroundDrawableSetter(viewId,
68 | drawableId));
69 | return this;
70 | }
71 |
72 | /**
73 | * 设置文本颜色,因此View的类型必须为TextView或者其子类
74 | *
75 | * @param viewId
76 | * @param colorId
77 | * @return
78 | */
79 | public ViewGroupSetter childViewTextColor(int viewId, int colorId) {
80 | mItemViewSetters.add(new TextColorSetter(viewId, colorId));
81 | return this;
82 | }
83 |
84 | @Override
85 | public void setValue(Theme newTheme, int themeId) {
86 | mView.setBackgroundColor(getColor(newTheme));
87 | // 清空AbsListView的元素
88 | clearListViewRecyclerBin(mView);
89 | // 清空RecyclerView
90 | clearRecyclerViewRecyclerBin(mView);
91 | // 修改所有子元素的相关属性
92 | changeChildenAttrs((ViewGroup) mView, newTheme, themeId);
93 | }
94 |
95 | /**
96 | *
97 | * @param viewId
98 | * @return
99 | */
100 | private View findViewById(View rootView, int viewId) {
101 | View targetView = rootView.findViewById(viewId);
102 | Log.d("", "### viewgroup find view : " + targetView);
103 | return targetView;
104 | }
105 |
106 | /**
107 | * 修改子视图的对应属性
108 | *
109 | * @param viewGroup
110 | * @param newTheme
111 | * @param themeId
112 | */
113 | private void changeChildenAttrs(ViewGroup viewGroup, Theme newTheme,
114 | int themeId) {
115 | int childCount = viewGroup.getChildCount();
116 | for (int i = 0; i < childCount; i++) {
117 | View childView = viewGroup.getChildAt(i);
118 | // 深度遍历
119 | if (childView instanceof ViewGroup) {
120 | changeChildenAttrs((ViewGroup) childView, newTheme, themeId);
121 | }
122 |
123 | // 遍历子元素与要修改的属性,如果相同那么则修改子View的属性
124 | for (ViewSetter setter : mItemViewSetters) {
125 | // 每次都要从ViewGroup中查找数据
126 | setter.mView = findViewById(viewGroup, setter.mViewId);
127 |
128 | Log.e("", "### childView : " + childView + ", id = "
129 | + childView.getId());
130 | Log.e("", "### setter view : " + setter.mView + ", id = "
131 | + setter.getViewId());
132 | if (childView.getId() == setter.getViewId()) {
133 | setter.setValue(newTheme, themeId);
134 | Log.e("", "@@@ 修改新的属性: " + childView);
135 | }
136 | }
137 | }
138 | }
139 |
140 | private void clearListViewRecyclerBin(View rootView) {
141 | if (rootView instanceof AbsListView) {
142 | try {
143 | Field localField = AbsListView.class
144 | .getDeclaredField("mRecycler");
145 | localField.setAccessible(true);
146 | Method localMethod = Class.forName(
147 | "android.widget.AbsListView$RecycleBin")
148 | .getDeclaredMethod("clear", new Class[0]);
149 | localMethod.setAccessible(true);
150 | localMethod.invoke(localField.get(rootView), new Object[0]);
151 | Log.e("", "### 清空AbsListView的RecycerBin ");
152 | } catch (NoSuchFieldException e1) {
153 | e1.printStackTrace();
154 | } catch (ClassNotFoundException e2) {
155 | e2.printStackTrace();
156 | } catch (NoSuchMethodException e3) {
157 | e3.printStackTrace();
158 | } catch (IllegalAccessException e4) {
159 | e4.printStackTrace();
160 | } catch (InvocationTargetException e5) {
161 | e5.printStackTrace();
162 | }
163 | }
164 | }
165 |
166 | private void clearRecyclerViewRecyclerBin(View rootView) {
167 | if (rootView instanceof RecyclerView) {
168 | try {
169 | Field localField = RecyclerView.class
170 | .getDeclaredField("mRecycler");
171 | localField.setAccessible(true);
172 | Method localMethod = Class.forName(
173 | "android.support.v7.widget.RecyclerView$Recycler")
174 | .getDeclaredMethod("clear", new Class[0]);
175 | localMethod.setAccessible(true);
176 | localMethod.invoke(localField.get(rootView), new Object[0]);
177 | Log.e("", "### 清空RecyclerView的Recycer ");
178 | rootView.invalidate();
179 | } catch (NoSuchFieldException e1) {
180 | e1.printStackTrace();
181 | } catch (ClassNotFoundException e2) {
182 | e2.printStackTrace();
183 | } catch (NoSuchMethodException e3) {
184 | e3.printStackTrace();
185 | } catch (IllegalAccessException e4) {
186 | e4.printStackTrace();
187 | } catch (InvocationTargetException e5) {
188 | e5.printStackTrace();
189 | }
190 | }
191 | }
192 |
193 | }
194 |
--------------------------------------------------------------------------------
/src/com/simple/colorful/setter/ViewSetter.java:
--------------------------------------------------------------------------------
1 | package com.simple.colorful.setter;
2 |
3 | import android.content.res.Resources.Theme;
4 | import android.util.TypedValue;
5 | import android.view.View;
6 |
7 | /**
8 | * ViewSetter,用于通过{@see #mAttrResId}
9 | * 设置View的某个属性值,例如背景Drawable、背景色、文本颜色等。如需修改其他属性,可以自行扩展ViewSetter.
10 | *
11 | * @author mrsimple
12 | *
13 | */
14 | public abstract class ViewSetter {
15 |
16 | /**
17 | * 目标View
18 | */
19 | protected View mView;
20 | /**
21 | * 目标view id,有时在初始化时还未构建该视图,比如ListView的Item View中的某个控件
22 | */
23 | protected int mViewId;
24 | /**
25 | * 目标View要的特定属性id
26 | */
27 | protected int mAttrResId;
28 |
29 | public ViewSetter(View targetView, int resId) {
30 | mView = targetView;
31 | mAttrResId = resId;
32 | }
33 |
34 | public ViewSetter(int viewId, int resId) {
35 | mViewId = viewId;
36 | mAttrResId = resId;
37 | }
38 |
39 | /**
40 | *
41 | * @param newTheme
42 | * @param themeId
43 | */
44 | public abstract void setValue(Theme newTheme, int themeId);
45 |
46 | /**
47 | * 获取视图的Id
48 | *
49 | * @return
50 | */
51 | protected int getViewId() {
52 | return mView != null ? mView.getId() : -1;
53 | }
54 |
55 | protected boolean isViewNotFound() {
56 | return mView == null;
57 | }
58 |
59 | /**
60 | *
61 | * @param newTheme
62 | * @param resId
63 | * @return
64 | */
65 | protected int getColor(Theme newTheme) {
66 | TypedValue typedValue = new TypedValue();
67 | newTheme.resolveAttribute(mAttrResId, typedValue, true);
68 | return typedValue.data;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------