Android style design is an important feature because it can personalize the widgets in applications. The style design is as follows:
Use XML resources to define styles in Android Projects
A single Android project can define multiple styles
Let the widget apply one of the styles
The style Function of Android is mainly intended for widgets. The style is to be applied to Widgets. In addition, Android provides the theme function to apply styles in a wider range.
The following is an example of a style definition:
Create a new file style. xml in the/res/values/directory. The edit content is as follows:
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "myText">
<Item name = "android: textSize"> 18sp </item>
<Item name = "android: textColor"> #00FF00 </item>
</Style>
<Style name = "myButton">
<Item name = "android: background"> #00 BFFF </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "myText">
<Item name = "android: textSize"> 18sp </item>
<Item name = "android: textColor"> #00FF00 </item>
</Style>
<Style name = "myButton">
<Item name = "android: background"> #00 BFFF </item>
</Style>
</Resources>
Style. xml syntax description:
In the <resource> label definition resource project, the <style> label is used to define the style resource;
<Style> the name attribute defines the style name, which is applied by the widget;
<Item> the tag defines the content of this style;
TextSize -- font size
TextColor -- font color
Background -- background
For more information, see Android Reference
After the style is defined, widgets can be applied.
It is easy to apply the defined style to Widgets. You only need to add the defined style name to the widget project attribute in main. XML. Edit main. XML:
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Style = "@ style/myText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<Button
Android: id = "@ + id/btn"
Style = "@ style/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Style = "@ style/myText"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<Button
Android: id = "@ + id/btn"
Style = "@ style/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Program Execution result:
From Young's column