Three lines of code to achieve smooth switchover during the day and night modes, three lines of code during the day

Source: Internet
Author: User

Three lines of code to achieve smooth switchover during the day and night modes, three lines of code during the day

Usage xml android: background =? Attr/zzbackground app: backgroundAttr = zzbackground // if you want to refresh the current page immediately, input the attribute name here, such as R. attr. zzbackground, and upload zzbackground to android: textColor =? Attr/zztextColor app: textColorAttr = zztextColor //

Demo Effect

Usage xml
Android: background = "? Attr/zzbackground "app: backgroundAttr =" zzbackground "// if you want to refresh the current page immediately, enter an attribute name, for example, R. attr. zzbackground, and set zzbackground to android: textColor = "? Attr/zztextColor "app: textColorAttr =" zztextColor "// if you need to refresh the page immediately, the effect is the same as above.
Java
@ Override protected void onCreate (Bundle savedInstanceState) {// call this method ChangeModeController on the page for immediate effect switching. 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. col1_cent); // ChangeModeController. getInstance (). addTextColor (view, R. attr. colorAccent); // set the switchover // ChangeModeController. changeDay (this, R. style. dayTheme); // ChangeModeController. changeNight (this, R. style. nightTheme) ;}@ Override protected void onDestroy () {super. onDestroy (); // call ChangeModeController in onDestroy. onDestory ();}
Detailed operation description Step 1: Customize attributes
<?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 2: configure the night style File
<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 = "col1_cent "> @ color/col1_cent </item> <item name =" windowActionBar "> false </item> <item name =" android: windowNoTitle "> true </item> <item name =" windowNoTitle "> true </item> </style> <! -- Daytime 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 name =" 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 = "col1_cent "> @ color/col1_centnight </item> </style> <style name =" AppTheme. appBarOverlay "parent =" ThemeOverlay. appCompat. dark. actionBar "/> <style name =" AppTheme. popupOverlay "parent =" ThemeOverlay. appCompat. light "/> </resources>

Set the attribute values of the corresponding mode for relevant properties:

<?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 3: configure the corresponding attribute 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_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 = "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 textColorAttr, backgroundAttr, and backgroundDrawableAttr attributes. If you need to refresh the current page immediately, add the corresponding attributes.

Attribute Description
TextColorAttr Set the font color. For example, you can use R. attr. zztextColor to set zztextColor. Example: app: textColorAttr = "zztextColor"
BackgroundAttr Set when you modify the background color or background image. Same as above. Example: app: backgroundAttr = "zzbackground"
BackgroundDrawableAttr Set when you modify the background color or background image. Same as above. Example: app: backgroundDrawableAttr = "zzbackground"
Step 4: Call java code on the page
@ Override protected void onCreate (Bundle savedInstanceState) {// 1. call this method ChangeModeController on the page for immediate effect switching. 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 to switch the night-time live daytime mode // ChangeModeController. changeDay (this, R. style. dayTheme); // switch the daytime mode // ChangeModeController. changeNight (this, R. style. nightTheme); // switch the night mode} @ Override protected void onDestroy () {super. onDestroy (); // 3. call ChangeModeController in onDestroy. onDestory ();}

You can start a night trip by calling the code in three steps.
If the page has a newly created view to be added to the night mode control, Android source code calls:

// Add additional view to night management // ChangeModeController. getInstance (). addBackgroundColor (toolbar, R. attr. colorPrimary); // ChangeModeController. getInstance (). addBackgroundDrawable (view, R. attr. col1_cent); // ChangeModeController. getInstance (). addTextColor (view, R. attr. col1_cent );

If you have other non-standard attributes when changing the night mode, you can call the following code to assign values to the attributes after ChangeModeController. changeDay or ChangeModeController. changeNight:

TypedValue attrTypedValue = ChangeModeController. getAttrTypedValue (this, R. attr. zztextColor); toolbar. setTitleTextColor (getResources (). getColor (attrTypedValue. resourceId); Source: http://www.codesocang.com/kj/transition/33588.html

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.