How to Set themes and custom themes in android

Source: Internet
Author: User

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

Related Article

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.