1:建立两种主题模式android
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="color_bg_main">@color/color_bg_main_light</item>
<item name="text_style">@style/Text_light_style</item>
<item name="second_text_style">@style/second_text_style_light</item>
</style>git
<style name="ThemeLight" parent="AppTheme">github
</style>ide
<style name="ThemeDark" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/color_primary</item>
<item name="colorPrimaryDark">@color/color_primary_dark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="color_bg_main">@color/color_bg_main_drak</item>
<item name="text_style">@style/Text_dark_style</item>
<item name="second_text_style">@style/second_text_style_dark</item>
</style>
<style name="Text_light_style">
<item name="android:textColor">@color/color_white</item>
<item name="android:background">@color/color_black</item>
</style>this
<style name="Text_dark_style">
<item name="android:textColor">@color/color_black</item>
<item name="android:background">@color/color_white</item>
</style>
<style name="second_text_style_light">
<item name="android:textColor">@color/color_white</item>
<item name="android:background">@color/color_black</item>
<item name="android:text">@string/text_intent_light</item>
</style>
<style name="second_text_style_dark">
<item name="android:textColor">@color/color_black</item>
<item name="android:background">@color/color_white</item>
<item name="android:text">@string/text_intent_drak</item>
</style>
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应该在哪定义呢???.net
第二步:定义自定义属性orm
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="color_bg_main" format="color|reference"/>
<attr name="text_style" format="reference"/>
<attr name="second_text_style" format="reference"/>
</resources>
1
2
3
4
5
6
定义完成以后须要引用才有效果,如何引用呢???xml
第三步:应用样式blog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">utf-8
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
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" />
<TextView
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" />
<TextView
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" />
</LinearLayout>
</LinearLayout>
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实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<TextView
style="?attr/second_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
/>
</LinearLayout>
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