A style is a set of attributes that specify the appearance and format for the view or window. A style can specify attributes such as height, margin, font color, font size, and background color. Styles are defined in an XML resource file separated from the layout XML file.
The style in Android is similar to the stacked style table in web design, allowing separate design from the content.
For example, you can use a style to convert the following layout XML into a declaration of a reference style:
Do not use style attributes:
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textcolor = "#00ff00"
Android: Typeface = "monospace"
Android: text = "@ string/hello"
/>
Use style attributes:
<Textview
Style = "@ style/codefont"
Android: text = "@ string/hello"
/>
In the above example, all style-related attributes are deleted from the layout XML, and these styles are placed in an XML style definition called codefont, and then set it to the style attribute.
A topic is a style between an application and an entire activity or application, rather than an independent view object. When a style is used as a topic, every view object in the activity or application uses each style attribute that it supports. For example, if the same codefont style is used as the topic of an activity, all texts in the activity will make the green font wide.
Define Style
To create a style set, you need to save an XML file to the res/values directory of the project directory. The XML file name is random, but must use the. xml extension and be saved in the Res/values folder.
The root node of the XML file must be <resources>
For each style to be created, A <style> element must be added to the XML file and the style must be uniquely identified by the permanent name attribute (this attribute is required ), add an <item> element to each attribute of the style. The name attribute of this element is used to declare the attribute name of the style. The attribute values are placed between a group of <items>. The value of the <item> element can be a string, a hexadecimal color, a reference to another resource type, or other values dependent on the style attribute, the following is an example file with a single style:
<? 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>
Each sub-element of the <resources> element must be converted into an application resource object during compilation and referenced by the name attribute value of the <style> element. The style in this example is referenced by @ style/codefont in the XML layout.
<Style> the parent attribute in the element is optional. It specifies the resource ID of another style. The current style can inherit the attribute of the specified style. If necessary, you can override the inherited style attributes.
Remember to use a style defined in XML as a theme of an activity or application, which is exactly the same as defining a style for a view object. For example, the style defined in the preceding example can be applied to a view object or used as the topic of an activity or application.
Style inheritance
<Style> the parent attribute in the element can inherit an existing style attribute, and then only define the attributes to be changed and added. You can inherit your own defined styles, or the built-in styles on the platform. For example, the following example inherits the default text appearance of the Android platform and modifies the text color:
<Stylename = "greentext" parent = "@ Android: style/textappearance">
<Itemname = "Android: textcolor"> #00ff00 </item>
</Style>
If you want to inherit a defined style, you do not need to use the parent attribute, but you need to prefix the style name you want to inherit as the new style name (separated by vertices ). For example, to create a new style that inherits the codefont style defined above, but to use red text, you can design the following new style:
<Stylename = "codefont. Red">
<Itemname = "Android: textcolor"> # ff0000 </item>
</Style>
Note that the <style> label does not contain the parent attribute, but because the name attribute value starts with the codefont style name (this style has been created), this style inherits all the styles defined by the codefont style, then it overwrites the Android: textcolor attribute and sets the text to red. Use @ style/codefont. Red to reference this style.
You can use vertices to connect names to achieve hierarchical style inheritance. For example, you can extend the above codefont. Red style to set the font style:
<Stylename = "codefont. Red. Big">
<Itemname = "Android: textsize"> 30sp </item>
</Style>
This style inherits the codefont and codefont. Red styles, and then adds the Android: textsize attribute.
Note:This type of inheritance that connects names is only valid for the style defined by the user. Android built-in styles cannot be inherited using this method. To reference a built-in style, such as a textappearance style, you must use the parent attribute.