当前位置: 移动技术网 > 移动技术>移动开发>Android > 三行Android代码实现白天夜间模式流畅切换

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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));

源码下载地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网