A style is a collections of properties that specify the look and format of a view or window.
They allow to separate the design from the content.
Without style XML, we can design it in the layout XML:
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textcolor = "#00ff00"
Android: Typeface = "monospace"
Android: text = "@ string/Hello"/>
With style XML, we can turn it:
<Textview
Style = "@ style/codefont"
Android: text = "@ string/Hello"/>
A theme is a style applied to an entire activity of application, rather than an individual view.
When a style is applied as a theme, every view in the activity or application will apply each style property that it supports.
Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it.
Defining styles
The root node of the XML file must be <resources>.
For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style.
Each child of the <resource> element is converted into an application resource object at compile-time.
Example:
<? 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>
Inheritance
The parent attribute in the <style> element lets you specify a style from which your style shoshould inherit properties.
Example
<Style name = "greentext" parent = "@ Android: style/textappearance">
<Item name = "Android: textcolor"> #00ff00 </item>
</Style>
Style Properties
The best place to find properties that apply to a specific view is the corresponding class reference, which lists all of the supported attributes.
Example
Java. Lang. Object
Using Android. R. ATTR
Public static final int textcolor
Added in API Level 1
Color of text (usually same as colorforeground ).
May be a reference to another resource, in the form"@ [+] [Package:]Type:Name
"Or to a theme attribute in the form"? [Package:] [Type:]Name
".
May be a color value, in the form"#RGB
","#Argb
","#Rrggbb
", Or"#Aarrggbb
".
Constant Value: 16842904 (0x01010098)