Three lines of Android code to achieve smooth switching between day and night modes
Usage xml android:background= ? Attr / zzbackground app: backgroundattr = zzbackground / / if the current page needs to be refreshed immediately, an attribute name such as r.attr Just pass zzbackground to Android: textcolor =? attr/zztextColor app:textColorAttr= zztextColor //
Demonstration effect
Usage xml
java
Detailed operation description
Step 1: Customize attributes
Step 2: configuration file
Set the attribute value of the corresponding mode for related attributes:
Step 3: configure and use the corresponding attributes in the layout file
Note the textcolorattr, backgroundattr, and backgrounddrawableattr attributes. If you want to refresh the current page immediately, you need to fill in the corresponding attributes.
Step 4: call java code on the page
The code calls three steps to start the night trip. If there is a newly created view on the page to add night mode control, the Android source code calls:
If there are other non-standard defined properties when changing the night mode, you can use changemodecontroller Changeday or changemodecontroller After changeNight, the following code is called to assign the relevant attributes: TypedValue attrTypedValue = ChangeModeController. getAttrTypedValue(this,R.attr.zztextColor); toolbar. setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
Source code download address: http://xiazai.jb51.net/201609/yuanma/AndroidChangeMode (jb51.net). rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.