Android Switch Theme mainstream three ways to switch theme, the first is the built-in style to switch, generally used for night mode/day mode switch. The second is to use the APK to achieve the plug-in, the third is to download the zip to extract to the appropriate app file, the application will need to read the file into memory. This is a way to introduce the first Android switching theme.
First of all, of course, create a attrs file under values, and then define some attr.
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "main" > < attr name= "BgColor" format= "color" ></attr> <attr name= "buttonbgcolor" format= "Color" ></attr > <attr name= "buttontextcolor" format= "color" ></attr> <attr name= "textSize" format= " Dimension "></attr> </declare-styleable> </resources>
Then define the two style in the styles file.
<style name= "Daytheme" > <item name= "BgColor" > #ffffff </item> <item name= "Buttonbgcolor" & GT, #80000000 </item> <item name= "Buttontextcolor" > #80ffffff </item> <item name= "TextSize" >14sp</item> </style> <style name= "Nighttheme" > <item name= "BgColor" > #cc0000 00</item> <item name= "Buttonbgcolor" > #80ffffff </item> <item name= "Buttontextcolor" ; #80000000 </item> <item name= "textSize" >14sp</item> </style>
The attrs is then used in the layout file.
<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 " android:background= "Attr/bgcolor" > <button android:id= "@+id/swtichthemebtn" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:textsize= "? attr/ TextSize " android:text=" Press the button to switch the night mode " android:layout_margin=" 10dip " android:background="? attr/ Buttonbgcolor " android:textcolor="? Attr/buttontextcolor " /></linearlayout>
The last is to set the theme in mainactivity and dynamically switch theme.
Import Android.os.bundle;import Android.preference.preferencemanager;import android.annotation.SuppressLint; Import Android.app.activity;import Android.content.intent;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.view.view;import Android.widget.button;public Class Mainactivity extends Activity {private Button mswtichthemebtn;private boolean isnight;private sharedpreferences sp;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); sp = Preferencemanager.getdefaultsharedpreferences (this); SetTheme ((Isnight = Sp.getboolean ("Isnight", false))? R.style.nighttheme:r.style.daytheme); Setcontentview (r.layout.activity_main); mswtichthemebtn = (Button) This.findviewbyid (R.ID.SWTICHTHEMEBTN); Mswtichthemebtn.settext (isnight? ") Toggle Daytime Mode ": Toggle Night Mode") Mswtichthemebtn.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View V) {Editor edit = Sp.edit (); Edit.putboolean ("Isnight",!isnight); Edit.commit (); Recreatefortheme ();}}); @SuppressLint ("Newapi") public void Recreatefortheme () {if (Android.os.Build.VERSION.SDK_INT >=) {this.recreate ( );} Else{this.finish (); StartActivity (new Intent (Mainactivity.this,mainactivity.class));}}