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
- <TextView
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: textColor = "#00FF00"
- Android: typeface = "monospace"
- 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
- <TextView
- Style = "@ style/CodeFont"
- 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
- <? XmlVersion = "1.0" encoding = "UTF-8"?>
- <Resources>
- <StyleName = "CodeFont" parent = "@ android: style/TextAppearance. Medium">
- <ItemName = "android: layout_width">Fill_parent</Item>
- <ItemName = "android: layout_height">Wrap_content</Item>
- <ItemName = "android: textColor">#00FF00</Item>
- <ItemName = "android: typeface">Monospace</Item>
- </Style>
- </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
- <StyleName = "GreenText" parent = "@ android: style/TextAppearance">
- <ItemName = "android: textColor">#00FF00</Item>
- </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
- <StyleName = "CodeFont. Red">
- <ItemName = "android: textColor"># FF0000</Item>
- </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
- <StyleName = "CodeFont. Red. Big">
- <ItemName = "android: textSize">30sp</Item>
- </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
- <TextView
- Style = "@ style/CodeFont"
- 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
- <ApplicationAndroid: theme = "@ style/CustomTheme">
- Or
- <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