Android 实现夜间模式的快速简单方法实例详解_Android

ChangeMode

项目地址:ChangeMode

Implementation of night mode for Android.

用最简单的方式实现夜间模式,支持ListView、RecyclerView。

Preview

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) {
//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);
//添加额外 view 至夜间管理
// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);
// ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
//2. 设置切换
//ChangeModeController.changeDay(this, R.style.DayTheme);
//ChangeModeController.changeNight(this, R.style.NightTheme);
}
@Override
protected void onDestroy() {
super.onDestroy();
//3. 在 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三个属性。如需当前页面立即刷新,需填加相应属性。

属性 描述

textColorAttr 修改字体颜色时设置。如 R.attr.zztextColor 传 zztextColor 即可。例:app:textColorAttr="zztextColor"
backgroundAttr 修改背景颜色/背景图片时设置。同上。例: app:backgroundAttr="zzbackground"
backgroundDrawableAttr 修改背景颜色/背景图片时设置。同上。例: app:backgroundDrawableAttr="zzbackground"

第四步:页面调用 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));
About me
An Android Developer in ZhengZhou.

License
======= Copyright 2016 zhangke
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

以上所述是小编给大家介绍的Android 实现夜间模式的快速简单方法实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对网站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
夜间模式
婴儿毛衣编织实例详解、abaqus工程实例详解、android创意实例详解、c 编程实例详解、usb应用开发实例详解,以便于您获取更多的相关知识。

时间: 2024-08-20 01:15:28

Android 实现夜间模式的快速简单方法实例详解_Android的相关文章

Android 实现夜间模式的快速简单方法实例详解

ChangeMode 项目地址:ChangeMode Implementation of night mode for Android. 用最简单的方式实现夜间模式,支持ListView.RecyclerView. Preview Usage xml android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"//如果当前页面要立即刷新,这里传入属性名称 比如 R.attr.zzb

Android编程之软键盘的隐藏显示实例详解_Android

本文实例分析了Android编程之软键盘的隐藏显示方法.分享给大家供大家参考,具体如下: Android是一个针对触摸屏专门设计的操作系统,当点击编辑框,系统自动为用户弹出软键盘,以便用户进行输入. 那么,弹出软键盘后必然会造成原有布局高度的减少,那么系统应该如何来处理布局的减少?我们能否在应用程序中进行自定义的控制?这些是本文要讨论的重点. 一.软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog! InputMethodService为我们的输入法创建了一个Dialog,并且将

Android编程之菜单的实现方法实例详解_Android

本文实例讲述了Android编程之菜单的实现方法.分享给大家供大家参考,具体如下: Options Menu 当用户按下menu button按钮时显示的菜单 Context Menu 当用户长久按住屏幕,被注册显示上下文菜单的视图时显示的菜单 Submenu    当用户按下一个菜单的某个选项时弹出的子菜单 以上三种菜单也就是我们经常所说的:选项菜单,上下文菜单和子菜单. 一.上下文菜单 1. 实现onCreateContextMenu即可创建该菜单 @Override public void

Android发送邮件的方法实例详解_Android

本文实例讲述了Android发送邮件的方法.分享给大家供大家参考,具体如下: 在android手机中实现发送邮件的功能也是不可缺少的.如何实现它呢?下面以简单的例子进行说明. 程序如下: import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import

Android发送短信方法实例详解_Android

本文实例讲述了Android发送短信方法.分享给大家供大家参考,具体如下: 短信和打电话一样,都是android手机的基本功能,下面以实例说明android如何实现发送短信的功能. 程序如下所示: import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.app.PendingIntent; import android.content.I

Android编程设置全屏的方法实例详解_Android

本文实例讲述了Android编程设置全屏的方法.分享给大家供大家参考,具体如下: 在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果.其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏. 其一:在代码中设置(如下) package xiaohang.zhimeng; import android.app.Activity; import android.content.pm.ActivityInfo; i

Android 跨进程模拟按键(KeyEvent )实例详解_Android

  Android 解决不同进程发送KeyEvent 的问题 最近在做有关于Remote Controller 的功能,该功能把手机做成TV的遥控器来处理.在手机的客户端发送消息到TV的android 服务端,服务端接收到客户端的请求消息,模拟KeyEvent命令,发送Key值.  最简单的发送命令为如下代码: public static void simulateKeystroke(final int KeyCode) { new Thread(new Runnable() { public

Android编程中activity的完整生命周期实例详解_Android

本文实例分析了Android编程中activity的完整生命周期.分享给大家供大家参考,具体如下: android中 activity有自己的生命周期,对这些知识的学习可以帮助我们在今后写程序的时候,更好的理解其中遇到的一些错误.这篇文章很长,希望不要耽误大家的时间- 今天不会涉及太多关于activity栈的东西,主要说activity自身的生命周期 区分几个概念 1 Activity 官方解释为 "An Activity is an application component that pro

Android ListView添加头布局和脚布局实例详解_Android

Android ListView添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考: 如果我们当前的页面有多个接口.多种布局的话,我们一般的选择无非就是1.多布局:2.各种复杂滑动布局外面套一层ScrollView(好low):3.头布局脚布局.有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了:学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了: 效果图:                  正文部分布局: fragme