欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-05 15:43:19
changemode 项目地址:changemode implementation of night mode for android. 用最简单的方式实现夜间模式,...

changemode

项目地址:changemode

implementation of night mode for android.

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

preview

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

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 实现夜间模式的快速简单方法实例详解,希望对大家有所帮助