Three lines of Android code to achieve daytime night mode fluent switch _android

Source: Internet
Author: User
Tags recyclerview android

Usage XML android:background=? attr/zzbackground app:backgroundattr= zzbackground//If the current page is to be refreshed immediately, pass the property name here For example, R.attr.zzbackground Chuan Zzbackground can be android:textcolor=? attr/zztextcolor app:textcolorattr= ZztextColor//

Demo effect

Usage XML

Android:background= "Attr/zzbackground"
 app:backgroundattr= "zzbackground"//If the current page is to be refreshed immediately, pass the property name here For example, R.attr.zzbackground Chuan Zzbackground can 
 android:textcolor= "? Attr/zztextcolor"
 app:textcolorattr= " Zztextcolor "///For Immediate Refresh page effect ditto 

Java

 @Override protected void OnCreate (Bundle savedinstancestate) {//Call this method on the page to which you want to switch effects immediately
   Changemodecontroller.getinstance (). Init (This,r.attr.class). Settheme (This, r.style.daytheme, r.style.nighttheme);
  Call this method on other pages//changemodecontroller.settheme (this, r.style.daytheme, r.style.nighttheme);
  Super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_main);
  Add additional view to night Management//Changemodecontroller.getinstance (). Addbackgroundcolor (toolbar, r.attr.colorprimary);
  Changemodecontroller.getinstance (). addbackgrounddrawable (view,r.attr.coloraccent);


  Changemodecontroller.getinstance (). Addtextcolor (view,r.attr.coloraccent);
  Set Toggle//changemodecontroller.changeday (this, r.style.daytheme);
 Changemodecontroller.changenight (this, r.style.nighttheme);
  } @Override protected void OnDestroy () {Super.ondestroy ();
 Call Changemodecontroller.ondestory () in OnDestroy; } 

Detailed Operation description

First step: Custom Properties

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

Step Two: Configure the nightly style file

<resources> <!--Base application theme. --> <style name= "Apptheme" parent= "Theme.AppCompat.Light.DarkActionBar" > <!--Customize your here. --> <item name= "colorprimary" > @color/colorprimary</item> <item name= "Colorprimarydark" > @color /colorprimarydark</item> <item name= "coloraccent" > @color/coloraccent</item> <item " Windowactionbar ">false</item> <item name=" Android:windownotitle ">true</item> <item name=" Windownotitle ">true</item> </style> <!--day mode--> <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 " Zzitembackground "> @color/dayitembackground</item> </style> <!--night mode--> <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>

Set the attribute value for the corresponding pattern for the related property:

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

Step Three: configure the use of corresponding properties in the layout file

 <?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_he ight= "Attr/actionbarsize" android:background= "Attr/colorprimary" app:backgroundattr= "ColorPrimary" App:titleTex Tcolor= "Attr/zztextcolor" app:popuptheme= "@style/apptheme.popupoverlay"/> </android.support.design.widget.appbarlayout> <button android:layout_width= "match_parent" android:layout_height= "120DP" and roid: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=" night mode switch by Mr.zk "/> <android.suppor T.v7.widget.recyclerview android:id= "@+id/recyclerview" android:layout_width= "Match_parent" android:layout_height 

 = "Match_parent" android:scrollbars= "vertical"/> </LinearLayout>

Note Textcolorattr, Backgroundattr, backgrounddrawableattr three properties. If you want the current page to refresh immediately, you need to fill in the appropriate attributes.

Fourth Step: page invoke Java code

 @Override
 protected void onCreate (Bundle savedinstancestate) {
   //1. Call this method
   on the page that you want to switch effects immediately Changemodecontroller.getinstance (). Init (This,r.attr.class). Settheme (This, r.style.daytheme, r.style.nighttheme);
   Call this method on other pages 
   //changemodecontroller.settheme (this, r.style.daytheme, r.style.nighttheme);
  Super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);

  2. Set switch Night Live daytime mode
  //changemodecontroller.changeday (this, r.style.daytheme);//Switch Day mode
  // Changemodecontroller.changenight (this, r.style.nighttheme)//Toggle Night Mode
 }
 @Override
 protected void OnDestroy () {
  Super.ondestroy ();
  3. Call
  changemodecontroller.ondestory () in OnDestroy;
  

The code calls three steps to start the night trip.
If the page has a newly created view to add Night mode control, An Zhouyuan code calls:

   Add additional view to night management
  //Changemodecontroller.getinstance (). Addbackgroundcolor (toolbar, r.attr.colorprimary);
  Changemodecontroller.getinstance (). addbackgrounddrawable (view,r.attr.coloraccent);
  Changemodecontroller.getinstance (). Addtextcolor (view,r.attr.coloraccent); 

If there are other non-standard defined properties when you change the night mode, you can call the following code after Changemodecontroller.changeday or changemodecontroller.changenight to assign a value to the related property:
Typedvalue Attrtypedvalue = Changemodecontroller.getattrtypedvalue (this, r.attr.zztextcolor);
Toolbar.settitletextcolor (Getresources (). GetColor (Attrtypedvalue.resourceid));

SOURCE Download Address: Http://xiazai.jb51.net/201609/yuanma/AndroidChangeMode (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.