Android custom style theme style)

Source: Internet
Author: User

Like styles in HTML/CSS, android can also use custom styles.

Create a styles. xml file under the value folder.

A style is a set of display attributes of a View or window. A style can specify attributes such as height, fill, font color, font size, and background color. A style is an XML resource file separated from the layout file. Styles in Android are like css style sheets in Web development. They use our styles for content-independent design and development.
For example, you can use a style to make the following layout File

Xml Code
  1. <TextView
  2. Android: layout_width = "fill_parent"
  3. Android: layout_height = "wrap_content"
  4. Android: textColor = "#00FF00"
  5. Android: typeface = "monospace"
  6. Android: text = "@ string/hello"/>
<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: textColor = "#00FF00" android: typeface = "monospace" android: text = "@ string/hello"/>

Simplified

Xml Code
  1. <TextView
  2. Style = "@ style/CodeFont"
  3. Android: text = "@ string/hello"/>
<TextView style = "@ style/CodeFont" android: text = "@ string/hello"/>

All style-related attributes are moved from the layout XML file to a style definition named "CodeFont", and a style attribute is used to specify the style name. You will see how to define a style in the following content.

The style applied to an Activity or application is called theme, instead of a View. All views in the current Activity or application apply the same theme style. For example, if you want an Activity to use the "CodeFont" topic, the text of all views in the Activity will be in the green font.

 

Define Style

To define a style, you must create an XML file under the res/values/directory. The file name is named by yourself, but it must be suffixed with. xml. The root node of Xml must be.
We use the style label to define a style and the label to define style attributes. As follows:

Xml Code
  1. <? XmlVersion = "1.0" encoding = "UTF-8"?>
  2. <Resources>
  3. <StyleName = "CodeFont" parent = "@ android: style/TextAppearance. Medium">
  4. <ItemName = "android: layout_width">Fill_parent</Item>
  5. <ItemName = "android: layout_height">Wrap_content</Item>
  6. <ItemName = "android: textColor">#00FF00</Item>
  7. <ItemName = "android: typeface">Monospace</Item>
  8. </Style>
  9. </Resources>
<? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <style name = "CodeFont" parent = "@ android: style/TextAppearance. medium "> <item name =" android: layout_width "> fill_parent </item> <item name =" android: layout_height "> wrap_content </item> <item name =" android: textColor "># 00FF00 </item> <item name =" android: typeface "> monospace </item> </style> </resources>

The name attribute of the Style label is mandatory. A node can define the color, height, or reference of another resource. The child nodes of all nodes are used as a resource of the application during compilation. Therefore, we can reference this resource through the name attribute value of the style node. For example, use @ style/CodeFont in the layout file to reference this style. The parent attribute is optional. It is used to identify which style the style inherits. All attributes in the parent style will be applied to the style, at the same time, the style in the parent style can be overwritten (similar to the inheritance of java ).

 

Style inheritance
There are two ways to implement style inheritance. As shown in the above example, we can use the parent attribute to inherit the style when defining the style, we can inherit a style we have defined, or a style that comes with the android platform (this article will introduce all the styles that come with the android platform later ). As shown below, we inherit the default text style of the android platform and change its font color to the color we need.

Xml Code
  1. <StyleName = "GreenText" parent = "@ android: style/TextAppearance">
  2. <ItemName = "android: textColor">#00FF00</Item>
  3. </Style>
<Style name = "GreenText" parent = "@ android: style/TextAppearance"> <item name = "android: textColor" >#00ff00 </item> </style>

Another inheritance method is to use a custom style as the prefix. This method applies only to inheriting User-Defined styles. As follows:

Xml Code
  1. <StyleName = "CodeFont. Red">
  2. <ItemName = "android: textColor"># FF0000</Item>
  3. </Style>
<Style name = "CodeFont. Red"> <item name = "android: textColor" >#ff0000 </item> </style>

In this way, the newly defined style inherits all the attributes of the CodeFont style and changes the font color to # FF0000. We can reference the new style: @ style/CodeFont. Red
The same method can be inherited as follows:

Xml Code
  1. <StyleName = "CodeFont. Red. Big">
  2. <ItemName = "android: textSize">30sp</Item>
  3. </Style>
<Style name = "CodeFont. Red. Big"> <item name = "android: textSize"> 30sp </item> </style>

In this way, the size of the new style document is different from that of CodeFont. Red.

Style attributes

So far, you know how to define a style. You also need to know how many style attributes can be defined by nodes. You may already be familiar with some of them, such as layout_width and textColor. Of course, there are many style attributes that you can use.

The best way to query which style Attributes are supported by a view is to query the XML attributes table in the View class document, as shown in the link for XML Attributes in TextView: http://www.ideasandroid.com/android/sdk/docs/reference/android/widget/TextView.html#lattrs

For all available style properties, see R. attr: http://www.ideasandroid.com/android/sdk/docs/reference/android/R.attr.html
Not all views support the above style attributes. If you encounter unsupported style attributes, the attributes you define will be ignored.

Application style and topic

The method for applying styles and themes is simple. In layout, you can use the style attribute to directly reference style resources, as shown below:

Xml Code
  1. <TextView
  2. Style = "@ style/CodeFont"
  3. Android: text = "@ string/hello"/>
<TextView style = "@ style/CodeFont" android: text = "@ string/hello"/>

You can reference a style topic in the AndroidManifest. xml file. You can define a topic for an Activity or the entire application, as shown below:

Xml Code
  1. <ApplicationAndroid: theme = "@ style/CustomTheme">
  2. Or
  3. <ActivityAndroid: theme = "@ style/CustomDialogTheme">
<Application android: theme = "@ style/CustomTheme"> or <activity android: theme = "@ style/CustomDialogTheme">

Android platform styles and themes

Style see http://www.ideasandroid.com/android/sdk/styles.xml
Theme see: http://www.ideasandroid.com/android/sdk/themes.xml

 

Reprinted please indicate the source http://www.ideasandroid.com/archives/322

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.