1:创建两种主题模式
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
@color/color_bg_main_light
@style/Text_light_style
@style/second_text_style_light
@color/color_primary
@color/color_primary_dark
@color/colorAccent
@color/color_bg_main_drak
@style/Text_dark_style
@style/second_text_style_dark
@color/color_white
@color/color_black
@color/color_black
@color/color_white
@color/color_white
@color/color_black
@string/text_intent_light
@color/color_black
@color/color_white
@string/text_intent_drak
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
那么其中的一些text_style,second_text_style应该在哪定义呢???
第二步:定义自定义属性
1
2
3
4
5
6
定义完成之后需要引用才有效果,如何引用呢???
第三步:应用样式
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/color_bg_main"
android:padding="16dp"
tools:context="com.example.themechangeandroid.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/tv_light"
style="?attr/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="16dp"
android:text="@string/text_light" />
android:id="@+id/tv_dark"
style="?attr/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="left"
android:padding="16dp"
android:text="@string/text_dark" />
android:id="@+id/tv_intent"
style="?attr/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:gravity="left"
android:padding="16dp"
android:text="@string/text_intent" />
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
activity_second.xml实现
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="?attr/color_bg_main"
tools:context="com.example.themechangeandroid.SecondActivity">
style="?attr/second_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
通过?attr/ 去引用样式
第四步:切换主题
应用某个主题的时候,在setContentView方法之前才有效果,即如下设置主题
@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.tv_light).setOnClickListener(this);
findViewById(R.id.tv_dark).setOnClickListener(this);
findViewById(R.id.tv_intent).setOnClickListener(this);
}
1
2
3
4
5
6
7
8
9
设想,我们需要把所有界面统一设置主题,那么每个Activity都需要调用 ThemeUtil.setTheme(this);修改之后如何知道当前是何主题呢?
可以设置一个标志,保存到本地,当每打开一个界面时,需要根据标志位来设置主题,如下
public static void setTheme(@NonNull Activity activity) {
boolean isLight = PrefsUtils.read(activity, Config.THEME_CONFIG, true);
activity.setTheme(isLight ? R.style.ThemeLight : R.style.ThemeDark);
}
1
2
3
4
当切换主题的时候,如何修改当前页面主题呢?上述我们知道,setTheme在setContentView之前调用,除非我们重新走oncrete方法,否则无法刷新本页面主题样式,我们可以调用recreate方法,重走oncrete方法,原理是销毁并重新创建当前Activity。
public static void reCreate(@NonNull final Activity activity) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.recreate();
}
});
}
1
2
3
4
5
6
7
8
9
当点击切换时,需要先修改保存标志位,然后调用recrete方法
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_light:
PrefsUtils.write(this,Config.THEME_CONFIG,true);
ThemeUtil.reCreate(this);
break;
case R.id.tv_dark:
PrefsUtils.write(this,Config.THEME_CONFIG,false);
ThemeUtil.reCreate(this);
break;
case R.id.tv_intent:
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
然后得到如下效果:
代码地址:
github代码地址
参考资料:https://github.com/TakWolf/CNode-Material-Desig
————————————————
版权声明:本文为CSDN博主「隔壁小王66」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16131393/article/details/77480315