微信公众号

Android更换主题

来自开源项目:https://github.com/lguipeng/Notes,其中有改变主题的功能,我做了部分修改,并应用到我的APP中。感谢lguipeng的开源。

效果预览


实例APK

样式定义

style.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="ChangeThemeDialog" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowEnterAnimation">@anim/abc_fade_in</item>
<item name="android:windowExitAnimation">@anim/abc_fade_out</item>
</style>
<style name="RedTheme" parent="AppTheme">
<item name="colorPrimary">@color/red_primary</item>
<item name="colorPrimaryDark">@color/red_primary_dark</item>
<item name="colorAccent">@color/red_accent</item>
</style>
<style name="PinkTheme" parent="AppTheme">
<item name="colorPrimary">@color/pink_primary</item>
<item name="colorPrimaryDark">@color/pink_primary_dark</item>
<item name="colorAccent">@color/pink_accent</item>
</style>
<style name="BrownTheme" parent="AppTheme">
<item name="colorPrimary">@color/brown_primary</item>
<item name="colorPrimaryDark">@color/brown_primary_dark</item>
<item name="colorAccent">@color/brown_accent</item>
</style>
<style name="BlueTheme" parent="AppTheme">
<item name="colorPrimary">@color/blue_primary</item>
<item name="colorPrimaryDark">@color/blue_primary_dark</item>
<item name="colorAccent">@color/blue_accent</item>
</style>
<style name="BlueGreyTheme" parent="AppTheme">
<item name="colorPrimary">@color/blue_grey_primary</item>
<item name="colorPrimaryDark">@color/blue_grey_primary_dark</item>
<item name="colorAccent">@color/blue_grey_accent</item>
</style>
<style name="YellowTheme" parent="AppTheme">
<item name="colorPrimary">@color/yellow_primary</item>
<item name="colorPrimaryDark">@color/yellow_primary_dark</item>
<item name="colorAccent">@color/yellow_accent</item>
</style>
<style name="DeepPurpleTheme" parent="AppTheme">
<item name="colorPrimary">@color/deep_purple_primary</item>
<item name="colorPrimaryDark">@color/deep_purple_primary_dark</item>
<item name="colorAccent">@color/deep_purple_accent</item>
</style>
<style name="GreenTheme" parent="AppTheme">
<item name="colorPrimary">@color/green_primary</item>
<item name="colorPrimaryDark">@color/green_primary_dark</item>
<item name="colorAccent">@color/green_accent</item>
</style>
<style name="DeepOrangeTheme" parent="AppTheme">
<item name="colorPrimary">@color/deep_orange_primary</item>
<item name="colorPrimaryDark">@color/deep_orange_primary_dark</item>
<item name="colorAccent">@color/deep_orange_accent</item>
</style>
<style name="GreyTheme" parent="AppTheme">
<item name="colorPrimary">@color/grey_primary</item>
<item name="colorPrimaryDark">@color/grey_primary_dark</item>
<item name="colorAccent">@color/grey_accent</item>
</style>
<style name="CyanTheme" parent="AppTheme">
<item name="colorPrimary">@color/cyan_primary</item>
<item name="colorPrimaryDark">@color/cyan_primary_dark</item>
<item name="colorAccent">@color/cyan_accent</item>
</style>
<style name="AmberTheme" parent="AppTheme">
<item name="colorPrimary">@color/amber_primary</item>
<item name="colorPrimaryDark">@color/amber_primary_dark</item>
<item name="colorAccent">@color/amber_accent</item>
</style>

颜色定义

color.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<color name="red_primary">#F44336</color>
<color name="red_primary_dark">#D32F2F</color>
<color name="red_accent">#F44336</color>
<color name="pink_primary">#E91E63</color>
<color name="pink_primary_dark">#C2185B</color>
<color name="pink_accent">#E91E63</color>
<color name="brown_primary">#795548</color>
<color name="brown_primary_dark">#5D4037</color>
<color name="brown_accent">#795548</color>
<color name="blue_primary">#2196F3</color>
<color name="blue_primary_dark">#1976D2</color>
<color name="blue_accent">#2196F3</color>
<color name="blue_grey_primary">#607D8B</color>
<color name="blue_grey_primary_dark">#455A64</color>
<color name="blue_grey_accent">#607D8B</color>
<color name="yellow_primary">#FFEB3B</color>
<color name="yellow_primary_dark">#FBC02D</color>
<color name="yellow_accent">#FFEB3B</color>
<color name="deep_purple_primary">#673AB7</color>
<color name="deep_purple_primary_dark">#512DA8</color>
<color name="deep_purple_accent">#673AB7</color>
<color name="green_primary">#4CAF50</color>
<color name="green_primary_dark">#388E3C</color>
<color name="green_accent">#4CAF50</color>
<color name="deep_orange_primary">#FF5722</color>
<color name="deep_orange_primary_dark">#E64A19</color>
<color name="deep_orange_accent">#FF5722</color>
<color name="grey_primary">#9E9E9E</color>
<color name="grey_primary_dark">#616161</color>
<color name="grey_accent">#9E9E9E</color>
<color name="cyan_primary">#00BCD4</color>
<color name="cyan_primary_dark">#0097A7</color>
<color name="cyan_accent">#00BCD4</color>
<color name="amber_primary">#FFC107</color>
<color name="amber_primary_dark">#FFA000</color>
<color name="amber_accent">#FFC107</color>
<color name="primary">#2196F3</color>
<color name="primary_dark">#1E88E5</color>
<color name="accent">@color/primary</color>

基类BaseActivity

初始化主题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class BaseActivity extends AppCompatActivity {
protected PreferenceUtils preferenceUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
preferenceUtils = PreferenceUtils.getInstance(this);
initTheme();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
}
private void initTheme() {
ThemeUtils.Theme theme = ThemeUtils.getCurrentTheme(this);
ThemeUtils.changTheme(this, theme);
}
}

调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
showThemeChooseDialog();
}
}
);
}
private void showThemeChooseDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mAtivity, R.style.ChangeThemeDialog);
builder.setTitle("更换主题");
Integer[] res = new Integer[]{R.drawable.red_round, R.drawable.brown_round, R.drawable.blue_round,
R.drawable.blue_grey_round, R.drawable.yellow_round, R.drawable.deep_purple_round,
R.drawable.pink_round, R.drawable.green_round, R.drawable.deep_orange_round,
R.drawable.grey_round, R.drawable.cyan_round, R.drawable.amber_round};
List<Integer> list = Arrays.asList(res);
ColorsListAdapter adapter = new ColorsListAdapter(mAtivity, list);
adapter.setCheckItem(ThemeUtils.getCurrentTheme(mAtivity).getIntValue());
GridView gridView = (GridView) LayoutInflater.from(mAtivity).inflate(R.layout.colors_panel_layout, null);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
gridView.setCacheColorHint(0);
gridView.setAdapter(adapter);
builder.setView(gridView);
final AlertDialog dialog = builder.show();
gridView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dialog.dismiss();
int value = ThemeUtils.getCurrentTheme(mAtivity).getIntValue();
Log.d("wxl", "value==" + value);
if (value != position) {
PreferenceUtils.setPreferenceInt(mAtivity, "change_theme_key", position);
mAtivity.recreate();
}
}
}
);
}
public class ColorsListAdapter extends BaseAdapter {
private int checkItem;
private Context context;
private List<Integer> list;
public ColorsListAdapter(Context context, List<Integer> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.colors_image_layout, null);
holder = new Holder();
holder.imageView1 = (ImageView) convertView.findViewById(R.id.img_1);
holder.imageView2 = (ImageView) convertView.findViewById(R.id.img_2);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.imageView1.setImageResource(list.get(position));
if (checkItem == position) {
holder.imageView2.setImageResource(R.mipmap.ic_done_white);
}
return convertView;
}
public void setCheckItem(int checkItem) {
this.checkItem = checkItem;
}
class Holder {
ImageView imageView1;
ImageView imageView2;
}
}
}

其中colors_panel_layout.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:numColumns="4"
android:padding="24dp"
android:background="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:layout_height="match_parent">
</GridView>

colors_image_layout.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/img_1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/img_2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"/>
</FrameLayout>

对话框圆形颜色
drawable下建red_round.xml,代码如下:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/red_primary"/>
</shape>

还有其他8个颜色,自己按照上面写写,这里代码不贴出了。

PreferenceUtils

SharedPreferences 封装类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class PreferenceUtils {
public static String getPreferenceString(Context context, String key,
final String defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getString(key, defaultValue);
}
public static void setPreferenceString(Context context, final String key,
final String value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putString(key, value).apply();
}
public static boolean getPreferenceBoolean(Context context, final String key,
final boolean defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getBoolean(key, defaultValue);
}
public static boolean hasKey(Context context, final String key) {
return PreferenceManager.getDefaultSharedPreferences(context).contains(
key);
}
public static void setPreferenceBoolean(Context context, final String key,
final boolean value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putBoolean(key, value).apply();
}
public static void setPreferenceInt(Context context, final String key,
final int value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putInt(key, value).apply();
}
public static int getPreferenceInt(Context context, final String key,
final int defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getInt(key, defaultValue);
}
public static void setPreferenceFloat(Context context, final String key,
final float value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putFloat(key, value).apply();
}
public static float getPreferenceFloat(Context context, final String key,
final float defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getFloat(key, defaultValue);
}
public static void setPreferenceLong(Context context, final String key,
final long value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putLong(key, value).apply();
}
public static long getPreferenceLong(Context context, final String key,
final long defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getLong(key, defaultValue);
}
public static void clearPreference(Context context,
final SharedPreferences p) {
final SharedPreferences.Editor editor = p.edit();
editor.clear();
editor.apply();
}
}

ThemeUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class ThemeUtils {
public static void changTheme(Activity activity, Theme theme) {
if (activity == null)
return;
int style = R.style.RedTheme;
switch (theme) {
case BROWN:
style = R.style.BrownTheme;
break;
case BLUE:
style = R.style.BlueTheme;
break;
case BLUE_GREY:
style = R.style.BlueGreyTheme;
break;
case YELLOW:
style = R.style.YellowTheme;
break;
case DEEP_PURPLE:
style = R.style.DeepPurpleTheme;
break;
case PINK:
style = R.style.PinkTheme;
break;
case GREEN:
style = R.style.GreenTheme;
break;
case DEEP_ORANGE:
style = R.style.DeepOrangeTheme;
break;
case GREY:
style = R.style.GreyTheme;
break;
case CYAN:
style = R.style.CyanTheme;
break;
case AMBER:
style = R.style.AmberTheme;
break;
default:
break;
}
activity.setTheme(style);
}
public static Theme getCurrentTheme(Context context) {
int value = PreferenceUtils.getPreferenceInt(context, "change_theme_key", 0);
return ThemeUtils.Theme.mapValueToTheme(value);
}
public enum Theme {
RED(0),
BROWN(1),
BLUE(2),
BLUE_GREY(3),
YELLOW(4),
DEEP_PURPLE(5),
PINK(6),
GREEN(7),
DEEP_ORANGE(8),
GREY(9),
CYAN(10),
AMBER(11);
private int mValue;
Theme(int value) {
this.mValue = value;
}
public static Theme mapValueToTheme(final int value) {
for (Theme theme : Theme.values()) {
if (value == theme.getIntValue()) {
return theme;
}
}
// If run here, return default
return RED;
}
static Theme getDefault() {
return RED;
}
public int getIntValue() {
return mValue;
}
}
}

完整demo

联系QQ1413129987

更新日志

  • 2016/12/13 修改
  • 2015/08/19 撰笔

由于多说和网易云跟帖评论服务相继关闭,来必力并不给力,因此本博客决定不再折腾评论,欢迎大家可以前往我的公众号留言交流!