三行Android代码实现白天夜间模式流畅切换
作者:天羽飞龙 发布时间:2021-06-11 08:15:12
Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //如果当前页面要立即刷新,这里传入属性名称 比如R.attr.zzbackground 传zzbackground即可 android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor //
演示效果
Usage xml
android:background="?attr/zzbackground"
app:backgroundAttr="zzbackground"//如果当前页面要立即刷新,这里传入属性名称 比如R.attr.zzbackground 传zzbackground即可
android:textColor="?attr/zztextColor"
app:textColorAttr="zztextColor"//如需立即刷新页面效果 同上
java
@Override
protected void onCreate(Bundle savedInstanceState) {
// 在要立即切换效果的页面调用此方法
ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
//在其他页面调用此方法
//ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//添加额外view至夜间管理
// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);
// ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
// 设置切换
//ChangeModeController.changeDay(this, R.style.DayTheme);
//ChangeModeController.changeNight(this, R.style.NightTheme);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在onDestroy调用
ChangeModeController.onDestory();
}
详细操作描述
第一步:自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="zzbackground" format="color|reference"/>
<attr name="zzbackgroundDrawable" format="reference"/>
<attr name="zztextColor" format="color"/>
<attr name="zzItemBackground" format="color"/>
</resources>
第二步:配置夜间style文件
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="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="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="windowNoTitle">true</item>
</style>
<!--日间模式 -->
<style name="DayTheme" parent="AppTheme">
<item name="zzbackground">@color/dayBackground</item>
<item name="zzbackgroundDrawable">@drawable/ic_launcher</item>
<item name="zztextColor">@color/dayTextColor</item>
<item name="zzItemBackground">@color/dayItemBackground</item>
</style>
<!--夜间模式 -->
<style name="NightTheme" parent="AppTheme">
<item name="zzbackground">@color/nightBackground</item>
<item name="zzbackgroundDrawable">@color/nightBackground</item>
<item name="zztextColor">@color/nightTextColor</item>
<item name="zzItemBackground">@color/nightItemBackground</item>
<item name="colorPrimary">@color/colorPrimaryNight</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
<item name="colorAccent">@color/colorAccentNight</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
为相关属性设置对应模式的属性值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="dayBackground">#F2F4F7</color>
<color name="dayTextColor">#000</color>
<color name="dayItemBackground">#fff</color>
<color name="nightItemBackground">#37474F</color>
<color name="nightBackground">#263238</color>
<color name="nightTextColor">#fff</color>
</resources>
第三步:在布局文件中配置使用对应属性
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="?attr/zzbackground"
app:backgroundAttr="zzbackground"
tools:context="com.thinkfreely.changemode.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:backgroundAttr="colorPrimary"
app:titleTextColor="?attr/zztextColor"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:textColor="?attr/zztextColor"
app:textColorAttr="zztextColor"
android:background="?attr/zzItemBackground"
app:backgroundAttr="zzItemBackground"
android:padding="10dp"
android:layout_marginBottom="8dp"
android:textSize="22sp"
android:textAllCaps="false"
android:text="夜间模式切换by Mr.Zk" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</LinearLayout>
注意textColorAttr、backgroundAttr、backgroundDrawableAttr三个属性。如需当前页面立即刷新,需填加相应属性。
第四步:页面调用java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
//1. 在要立即切换效果的页面调用此方法
ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
//在其他页面调用此方法
//ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2.设置切换夜间活日间模式
//ChangeModeController.changeDay(this, R.style.DayTheme);//切换日间模式
//ChangeModeController.changeNight(this, R.style.NightTheme);//切换夜间模式
}
@Override
protected void onDestroy() {
super.onDestroy();
//3. 在onDestroy调用
ChangeModeController.onDestory();
}
代码调用三步,即可开始夜间之旅。
如果页面有新创建的视图要加入夜间模式控制,安卓源码调用:
//添加额外view至夜间管理
// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);
// ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
如果在改变夜间模式时有其他非标准定义的属性时,可在ChangeModeController.changeDay或ChangeModeController.changeNight之后调用如下代码给相关属性赋值:
TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
源码下载地址:http://xiazai.jb51.net/201609/yuanma/AndroidChangeMode(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


猜你喜欢
- 本文实例讲述了C#实现Zip压缩目录中所有文件的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 从上一节的小demo中我们搞清楚了如何跟易宝对接以及易宝的支付流程。这一节我们来做一下支付页面以及在页面中导入银行图标。1. 存储银行图标银
- 前言:从MVC到WebApi,路由机制一直是伴随着这些技术的一个重要组成部分。它可以很简单:如果你仅仅只需要会用一些简单的路由,如/Home
- spring Cache注解和redis区别1.不支持TTL即不能设置过期时间 expires time,SpringCache 认为这是各
- 通过shift+shift可以调出搜索窗口或者ctrl+n但是,如果想搜索jdk中的类,只是在搜索栏中是无法搜出来的需要勾选 红框内的选项没
- 导入redis的jar包<!-- redis --> <dependency>  
- 最近的一个Android需要用到扫码功能,用的是Zxing开源库。Zxing的集成就不说了,但是Zxing默认的是横屏扫码,在实际生产中并不
- 全局配置无效依赖 <dependency> &n
- 1.application.yml中添加两个datasourceserver: port: 8080spring: application:
- ps:我用的版本是7.0.5场景:左侧第一列宽度不够,导致数据换行。Table table = new Table(new float[2]
- 本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下:今天在研究context的时候,对appl
- 一、树形结构树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是因为它看起来像一棵倒挂的树
- 1.过滤器:所谓过滤器顾名思义是用来过滤的,在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一
- 一、获取当前文件的路径1. System.Diagnostics.Process.GetCurrentProcess().MainModul
- 本文实例讲述了C#简单聊天程序实现方法。分享给大家供大家参考。具体如下:假如有服务器端程序,ChatServer和客户端程序ChatClie
- 本文讨论了Spring Data JDBC如何实现DDD中聚合根存储的设计思路,其中主要讨论了是不是每个实体都需要一个对应数据表,这种问题需
- 1.with 函数首先先从with函数开始,with函数接受两个参数,第一个参数可以是一个任意类型的对象,第二个参数是一个Lambda表达式
- Spring MVC高级技术包括但不限于web.xml配置、异常处理、跨重定向请求传递数据1、web.xml文件的配置<!DOCTYP
- Java提供的数据类型主要分为两大类:基本数据类型和引用数据类型。Java中的基本数据类型名称大小取值范围byte型 (字节)8bit-12
- 本文实例讲述了Java 线程的生命周期。分享给大家供大家参考,具体如下:一 代码/*** @Title: ThreadStatus.java