Android styles & themes

Source: Internet
Author: User

For more information, see the unlocking Android P90.

 

What is style and theme?
Style: a set of one or more formatting attributes. We can use it as a unit to use it in a single element of XML layout. For example, we can define a style to define the font size and color of the text, and then use it in a specific instance of the view element.

Theme: a set of one or more formatting attributes. We can use theme as a unit in all the activities of the application or an activity in the application. For example, we can define a theme, which defines a set of colors for the foreground and background of the window frame and panel, and defines the size and color attributes of the text for the menu, this theme can be applied to all the activities in your program.

Defines the structure of XML documents for styles and themes Resources

For each styles and themes, you can add a globally unique name to the <style> element or add a parent class attribute. Later, we can use this name to apply the style, and the parent class attribute identifies the style inherited by the current style. In the <style> element, declare one or more <items>, each <item> defines a name attribute, and defines the value of this style inside the element.

 

 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <! -- Define style --> <br/> <style name = "mytextstyle" mce_bogus = "1"> <item name = "Android: textsize "> 20px </item> <br/> <item name =" Android: textcolor "> # ec9237 </item> <br/> </style> <br/> <style name =" mytextstyle2 "mce_bogus =" 1 "> <item name =" android: textsize "> 14px </item> <br/> <item name =" Android: textcolor "> # ff7f7c </item> <br/> </style> </P> <p> <! -- Define theme --> <br/> <style name = "mytheme" mce_bogus = "1"> <item name = "Android: windownotitle "> true </item> <br/> <item name =" Android: textsize "> 14px </item> <br/> <item name =" Android: textcolor "> # ffff7f7c </item> <br/> </style> </P> <p> </resources>

From the above example, we can see that the style and theme are basically the same in definition, but they are used differently. In addition, some labels are used.

Style acts on view, and theme acts on application or activity.

Example

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Id = "@ + ID/linear" <br/> Android: orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> style = "@ style/mytextstyle" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: gravity = "center_vertical | center_horizontal" <br/> Android: TEXT = "@ string/Hello" <br/> </P> <p> <textview <br/> style = "@ style/mytextstyle2" <br/> android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: gravity = "center_vertical | center_horizontal" <br/> Android: TEXT = "www.google.cn" <br/> Android: autolink = "all" <br/> </linearlayout> <br/>

 

In this example, both style and theme are used, which may cause a conflict. When dealing with the conflict, I personally conclude that the style has a higher priority than theme.

One note is that Android: autolink defines it and its view will display blue-underlined text.

When you click this hyperlink, the system browser is called to start the webpage. At the same time, Android does not support HTML format.

 

@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settheme (R. style. mytheme); <br/> setcontentview (R. layout. main); </P> <p>}

Use theme in the Code:

The settheme () function calls the custom theme.

 

Apply theme in androidmanifest. xml:
To use theme in all the current activities, you can open androidmanifest. edit the <Application> tag to include the Android: Theme attribute. The value is the name of a topic, for example, <application Android: theme = "@ style/newtheme">.

If you only want an activity in the program to own this theme, you can modify the <activity> label. Android provides several built-in resources. You can switch between several theme types instead of writing them by yourself. For example, you can use theme in the dialog box to make your activity look like a dialog box. Defined in manifest, for example: <activity Android: theme = "@ Android: style/theme. Dialog">
If you like a theme but want to make some minor changes, you only need to add the theme as a parent. The android SDK provides us with a lot of ready-made theme,

For example, we modify theme. Dialog theme to inherit theme. dialog to generate a new theme. <Style parent = "@ Android: style/theme. Dialog"

After inheriting theme. Dialog, we can adjust theme as required. We can modify the value of each item element defined in theme. Dialog, and then use newdialogtheme instead of theme. Dialog in the android manifest file.

 

Differences between style and theme in usage

 

(Note that in this case style is not prefixed with the Android:
Namespace; it is a custom local style and not one provided by the platform ).
Styles can be taken one step further and used as themes. While a style refers to
Set of attributes applied to a single view element, themes refer to a set of attributes
Being applied to an entire screen. themes can be defined in exactly the same <style>
And <item> structure as styles are. To apply a theme you simply associate a style
An entire activity, such as: Android: theme = "@ Android: style/[stylename]".

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.