The purpose of this blog is to teach you to use the ANDROIDSDK support Lib to implement the app day/night mode switch, recently saw a lot of posts on this day and night mode switching open source project, in fact ANDROIDSDK Support has already had a very good support.
This demo download address at the end of the article, read the document if you can not achieve the download to play.
--------------------------------------------------------------------------------
Effect Demo
Left is the effect of Android 4.1, right is the effect of Android 6.0.
Implementation steps
I take my demo as an example, need to modify style, need to set different style or color for day/night, switch mode in Java code implementation.
First you need to create a new project and choose Baseactivity when you select the initial activity.
One, rely on AppCompat library
The Gradle in app module relies on AppCompat libraries, with a minimum version of 23.2.0:
dependencies {
compile ' com.android.support:appcompat-v7:23.4.0 '
}
Today's blog Switch day night mode is the principle of switching style, because our page refers to a variety of style, so we just for the definition of different style on it, a rough view of our layout page:
<?xml version= "1.0" encoding= "Utf-8"?> <android.support.design.widget.coordinatorlayout xmlns:android= " Http://schemas.android.com/apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools="
Http://schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent " Android:fitssystemwindows= "true" tools:context= "com.yanzhenjie.daynight.MainActivity" > < Android.support.design.widget.AppBarLayout android:layout_width= "match_parent" android:layout_height= "Wrap_ Content "Android: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:popuptheme=" @style/apptheme.popupoverlay "/> </ android.support.design.widget.appbarlayout> <include layout= "@layout/content_main"/> < Android.support.design.widget.FloatingActiOnbutton android:id= "@+id/fab" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Layout_gravity= "Bottom|end" android:layout_margin= "@dimen/fab_margin" android:src= "@android:d Rawable/ic_dialog_
Email "/> </android.support.design.widget.CoordinatorLayout>
Second, modify Style
Open the Res/values/styles.xml and put the original:
<style name= "Apptheme" parent= "Theme.AppCompat.Light.DarkActionBar" >
<item name= "Colorprimary" >@ color/colorprimary</item>
<item name= "Colorprimarydark" > @color/colorprimarydark</item>
<item name= "coloraccent" > @color/coloraccent</item>
</style>
To
<style name= "Apptheme" parent= "Theme.AppCompat.DayNight.DarkActionBar" >
<item name= "Colorprimary" > @color/colorprimary</item>
<item name= "Colorprimarydark" > @color/colorprimarydark</item >
<item name= "coloraccent" > @color/coloraccent</item>
</style>
That is, changing light to daynight,daynight or its subtopics supports the switch of White Night mode.
Iii. write different colors for different patterns
We notice the above Apptheme this theme is actually set three colors, here simple to achieve an effect, we modify these three colors, under the Res New Values-night folder:
Here to explain the role of this folder, our default mode is usually daytime mode, so the system will read values in the value, when we switch to night mode will read the value under Values-night, whether it is style or color. So we simply replace the color, then the new colors.xml, we first look at the values in the Colors.xml:
Then we copy the Colors.xml file in the values to the Values-night and modify the color:
I'll simply change the dark blue to blue, and change the rose to golden.
Ok,styel and color here on the technology, the following is the Java code switching mode.
Java code Control daytime Night Mode
First of all because this function comes from support-appcompat, so our activity is inherited appcompatactivity.
The following three modes can be used in initialization, or explicit invocation:
The first, automatic mode, if our app has location permissions, network permissions, and so on, the system can automatically really now night or day, when users open the app will automatically switch to the response mode, This pattern can be set by using a static code in app initialization or application:
{
Appcompatdelegate.setdefaultnightmode (Appcompatdelegate.mode_night_auto);
}
The second, daytime mode, invokes the recreate () method of the activity after invocation:
Getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_no);
Recreate ();
The third, night mode, calls the recreate () method of the activity after invocation:
Getdelegate (). Setlocalnightmode (Appcompatdelegate.mode_night_yes);
Recreate ();
All right, very rude, that's it, it's over.
Summarize
1. If you want to achieve more complex, not only in the values-night to set different colors, you can also build different style, to different view references.
2. Two places to note, one is that the app or activity reference style needs to be Theme.AppCompat.DayNight or its sub style, and the second is to invoke Getdelegate (). Setlocalnightmode () Your activity must be inherited from Appcompatactivity.
Article demo source download: Http://xiazai.jb51.net/201609/yuanma/AndroidDayNightSample (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.