Hey, there are two ways to set theme for android, one is to modify the manifest file, and the other is to modify our java code; the custom settings of a topic are similar to those of a style.
First, we will define two theme files. Add the following code in res/values/style. xml:
<Style name = "MyTheme1" parent = "@ android: style/Theme. Dialog">
</Style>
<Style name = "MyTheme2"
Parent = "@ android: style/Theme. Wallpaper. NoTitleBar. Fullscreen">
</Style>
<! --
Note:
Two theme styles are created. The first is the theme of the dialog box, and the second is the theme of the wallpaper. For example only, no
Change. To modify attributes, see themes. xml in the \ data \ res \ values directory of the android sdk.
Change the topic style in the file
-->
Then we set the topic by modifying the mainfest file and hit mainfest. add the <application> tag code android: theme = "@ style/MyTheme1" to the xml file. In this way, you can set the topic of the entire application. If you only modify the topic of an activity, this code can also be added to the corresponding <activity> tag.
In the java code, we implement the theme settings. The main code is as follows:
// If reading a topic fails, set it to the default topic.
Theme = getSharedPreferences ("cons", MODE_PRIVATE). getInt ("theme ",
Android. R. style. Theme );
// Set the topic
SetTheme (theme );
// Call the parent class method, which must be placed after the topic is set
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button btn = (Button) findViewById (R. id. btn1 );
// Click to switch the topic. Click it to restart the application to see the effect
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
If (R. style. MyTheme1! = Theme ){
// Save the topic to sharedPreference so that it can be read when you set the topic next time.
Sf. getSharedPreferences ("cons", Activity. MODE_PRIVATE). edit ()
. PutInt ("theme", R. style. MyTheme1). commit ();
} Else {
Sf. getSharedPreferences ("cons", Activity. MODE_PRIVATE). edit ()
. PutInt ("theme", R. style. MyTheme2). commit ();
}
// Exit the application
Android. OS. Process. killProcess (android. OS. Process. myPid ());
}
});
Program:
Project source code: Click me
Author: bausch