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

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

概述ChangeMode项目地址:ChangeModeImplementationofnightmodeforAndroid.用最简单的方式实现夜间模式,支持ListView、RecyclerView。

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

@OverrIDeprotected 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.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);}@OverrIDeprotected 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.AppbarLayoutandroID:layout_wIDth="match_parent"androID:layout_height="wrap_content"app:theme="@style/Apptheme.AppbarOverlay"><androID.support.v7.Widget.ToolbarandroID: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><buttonandroID: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:@R_419_5095@="10dp"androID:layout_marginBottom="8dp"androID:textSize="22sp"androID:textAllCaps="false"androID:text="夜间模式切换 by Mr.Zk" /><androID.support.v7.Widget.RecyclerVIEwandroID: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 代码

@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {//1. 在要立即切换效果的页面调用此方法ChangeModeController.getInstance().init(this,R.style.Nighttheme);super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);//2.设置切换夜间活日间模式//ChangeModeController.changeDay(this,R.style.Daytheme);//切换日间模式//ChangeModeController.changeNight(this,R.style.Nighttheme);//切换夜间模式}@OverrIDeprotected voID onDestroy() {super.onDestroy();//3. 在 onDestroy 调用ChangeModeController.onDestory();}

代码调用三步,即可开始夜间之旅。 如果页面有新创建的视图要加入夜间模式控制,代码调用:

//添加额外 vIEw 至夜间管理// ChangeModeController.getInstance().addBackgroundcolor(toolbar,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 实现夜间模式的快速简单方法实例详解全部内容,希望文章能够帮你解决Android 实现夜间模式的快速简单方法实例详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1148149.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存