android 黑白切换,Android实现黑白主题切换
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
android 黑白切换,Android实现黑白主题切换相关推荐
- ios 主题切换 思路_iOS 实现主题切换的最佳方案
投资证券的小伙伴儿,应该非常熟悉很多 App 的夜间模式.打开夜间模式时,App 所有的页面由白色变为黑色,这样做有两个好处:一个是可以保护眼睛:另一个是相关的红绿行情图像,也显得更加协调.Andro ...
- 【Web技术】1431- 总结前端主题切换的思考和现代前端样式的解决方案落地
关于本文 来自:codercao https://juejin.cn/post/7106702604024938503 demo在线体验地址:https://hongqingcao.github.io ...
- 【Web技术】1374- 纯 JS 实现灵活的前端主题切换功能
demo在线体验地址:https://hongqingcao.github.io/v-theme-colors/ 源码地址:https://github.com/HongqingCao/v-theme ...
- iOS开发夜间模式的设置(主题切换)
iOS开发夜间模式的设置(主题切换) 很长一段时间没有写博客了.想到自己最近刚好做了不少重构,刚好可以总结一下. 夜间模式,很多阅读类的或资讯类的App都会这个功能.以前自己也做过,现在把它抽出来封装 ...
- android自动切换暗色,超实用!Android 深色模式适配(可定时开启的APP内主题切换管理工具)...
前言 前面分享了一篇"黑白化主题"的文,主要适用场景是不久就要到来的"清明"等时节或者是其他的国家公祭日什么的(一名成熟的程序员,要学会自己提产品需求). 今天 ...
- Android主题切换功能
App一般会有多套颜色主题,下面介绍如何在App中添加主题切换功能: 先添加自定义属性,在values目录下新建attr.xml,根据主题中需要动态修改的颜色或者图片设定自定义属性 <?xml ...
- Android 换肤之旅——主题切换
随着手机应用的成熟发展,市面上的应用已不在以简单的实现功能为目标了,它们反而会更加注重用户体验.我们常说的换肤(主题)功能--针对用户的喜好来提供一个可选的主题也是提高用户体验的方式之一.换肤功能不仅 ...
- android 动态切换主题,android动态主题切换(RRO 技术)
android上的主题切换,Android从M开始加入了动态资源overlay机制 runtime resource overlay(RRO),这个是sony贡献的,实现机制如下图,就是在框架中建立一 ...
- 三个版本实现黑白主题切换
之前我们写过一个非常简单的黑白切换按钮,见链接 简单的js代码实现主题色切换 今天我们来继续写主题切换,最终实现效果如图 下面我们来看下代码如何实现更好: html部分: <header> ...
最新文章
- 微信小程序根据后台返回值设置自己想要的结果
- oracle技术之检查点及SCN深入研究
- 桌面程序开发入门(WinForm with C#)
- WPF实现下拉框带图文和水印
- Java学习笔记之 IO包 字符流
- 高效解析xml的总结,闲下来写的
- 剪了 20% 的刘海、120Hz 刷新率、1TB 存储,iPhone 13 来了!
- 嵌入式Linux初始化硬件RTC,嵌入式Linux系统中的快速启动技术研究
- Action+Service +Dao三层的功能划分
- 【实践】推荐召回体系化建设与排序优化实践(附PPT下载链接)
- Link Vision 打破传统视频监控模式,开启新型物联网智能视频服务
- 近期有哪些值得读的QA论文?
- IDC 机房空调问题解决方案
- 透视效果的十字路口,不再“亲人两行泪”
- 支付宝扫码支付php demo
- hdoj1814 Peaceful Commission【2-set】
- Linux下,为应用程序添加桌面图标(ubuntu18.4)
- 免冠证件照如何制作?制作证件照的简单方法
- 声纹识别之说话人验证speaker verification
- 一步一步在 Windows 10 用 visual studio 2019 编译 zmqpp 4.2.0 版
热门文章
- Python深度学习-Data Augmentation:使用Augly库进行图片数据增强
- 涉密计算机打印机共享案例分析,又碰到2个打印机无法共享的案例(打印机共享的四个步骤)...
- 怎样利用MAC中自带的截图工具
- 全新升级ADS-B地面接收机室外机 pingStation3
- 京东茅台抢购软件升级版(真正实现了完全自动化)
- jira与Jenkins集成
- 我在计算机教室的英语作文,我的新教室英语作文
- 瑞萨78K0/KB2系列单片机upd78F0503A学习心得1
- 京东app商品详情源数据接口(item_get-获得JD商品详情)
- OpenGL ES 3. 光照-散射光