Three lines of code implementation of the daytime night mode smooth switch implementation Library

Source: Internet
Author: User

Usage XML android:background= attr/zzbackground app:backgroundattr= zzbackground//If the current page is to be refreshed immediately, the property name is passed in such as R.attr.zzbackground Zzbackground can android:textcolor=? attr/zztextcolor app:textcolorattr= ZztextColor//

Demo effect

Usage XML
    android:background="?attr/zzbackground"    app:backgroundAttr="zzbackground"//如果当前页面要立即刷新,这里传入属性名称  比如R.attr.zzbackground  传zzbackground即可        android:textColor="?attr/zztextColor"    app:textColorAttr="zztextColor"//如需立即刷新页面效果 同上
Java
@OverrideProtectedvoidOnCreate(Bundle savedinstancestate) {Call this method Changemodecontroller.getinstance () on the page where you want to toggle the effect immediately. Init (This,r.attr.class). SetTheme (This, r.style.daytheme, r.style.nighttheme);Call this method on another pageChangemodecontroller.settheme (This, r.style.daytheme, r.style.nighttheme);Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);Add extra 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 Switch //changemodecontroller.changeday (this, r.style.daytheme); //changemodecontroller.changenight (this, r.style.nighttheme);} @Override protected void OnDestroy() { Super.ondestroy (); //In OnDestroy call Changemodecontroller.ondestory ();}            
Detailed Operation description First Step: Custom properties
<?xml version= "1.0" encoding= "Utf-8"?><Resources><attrName= "Zzbackground" format= "color| Reference "/> <attr name= "zzbackgrounddrawable" format= " Reference "/> <attr name= "Zztextcolor" format= "color"/> <attr name= " Zzitembackground "format=" color "/> </RESOURCES>        
Step Two: Configure the night style file
<Resources><!--Base Application theme. -<StyleName="Apptheme"Parent="Theme.AppCompat.Light.DarkActionBar" ><!--CustomizeYourThemeHere.-<ItemName= "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><!--Day Mode--<StyleName="Daytheme"Parent="Apptheme" ><ItemName= "Zzbackground ">@Color/daybackground</item> <item name="Zzbackgrounddrawable" > @drawable/ic_launcher</item> <item name="Zztextcolor" > @color/daytextcolor</item> <item name="Zzitembackground" > @color/dayitembackground</item></Style><!--night Mode--<StyleName="Nighttheme"Parent="Apptheme" ><ItemName= "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= "AppThemeparent=" themeoverlayRESOURCES>         

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

<?xml version= "1.0" encoding= "Utf-8"?><Resources><ColorName="Daybackground" > #F2F4F7</Color><ColorName="Daytextcolor" > #000</Color><ColorName="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 the corresponding property in the layout file
<?xml version= "1.0" encoding= "Utf-8"?><LinearLayoutXmlns: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:padding="10DP"android:layout_marginbottom= "8DP" android:textsize= "22SP"  Android:textallcaps= "false" android:text=  "night mode switch 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>        

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

Properties Description
Textcolorattr Set when the font color is modified. such as R.attr.zztextcolor pass Zztextcolor can. Example: app:textcolorattr= "Zztextcolor"
Backgroundattr Set when the background color/background picture is modified. Ditto. Example: app:backgroundattr= "Zzbackground"
Backgrounddrawableattr Set when the background color/background picture is modified. Ditto. Example: app:backgrounddrawableattr= "Zzbackground"
Fourth Step: page call Java code
@OverrideProtectedvoidOnCreate(Bundle savedinstancestate) {1. Call this method Changemodecontroller.getinstance () on the page where you want to toggle the effect immediately. 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)//Toggle Daytime mode //changemodecontroller.changenight (This, R.style.nighttheme);//Toggle Night Mode}  @Override protected void ondestroy  () {super.ondestroy (); //3. Call OnDestroy in Changemodecontroller.ondestory ();}      

The code calls three steps to start the Night tour.
If the page has a newly created view to add the night mode control, the Android source code is called:

         //添加额外view至夜间管理        // 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 changing the night mode, the following code can be called 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));源码:http://www.codesocang.com/kj/transition/33588.html

Three-line code implements a library for smooth day-and-night mode switching

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.