三行Android代码实现白天夜间模式流畅切换

三行Android代码实现白天夜间模式流畅切换,第1张

概述Usagexmlandroid:background=?attr/zzbackgroundapp:backgroundAttr=zzbackground//如果当前页面要立即刷新,这里传入属性名称比如R.attr.zzbackground传zzbackground即可android:textColor=?attr/zztextCol

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.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.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.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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的三行Android代码实现白天夜间模式流畅切换全部内容,希望文章能够帮你解决三行Android代码实现白天夜间模式流畅切换所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1148266.html

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

发表评论

登录后才能评论

评论列表(0条)

保存