A style resource defines the format and look for a UI. A style can be applied to an individual View
(from within a layout file) or to an entire Activity
or application (from within the manifest file).
For more information about creating and applying styles, please readStyles and Themes.
Note: A style is a simple resource that is referenced using the value provided in the name
attribute (not the name of the XML file). As such, you can combine style resources with other simple resources in the one XML file, under one <resources>
element.
-
FILE LOCATION:
-
res/values/filename.xml
The filename is arbitrary. The element'sname
will be used as the resource ID.
RESOURCE REFERENCE:
-
In XML:
@[package:]style/style_name
SYNTAX:
-
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="style_name" parent="@[package:]style/style_to_inherit"> <item name="[package:]style_property_name" >style_value</item> </style> </resources>
ELEMENTS:
- EXAMPLE:
-
-
XML file for the style (saved in
-
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomText" parent="@style/Text"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> </resources>
XML file that applies the style to a
-
<?xml version="1.0" encoding="utf-8"?> <EditText style="@style/CustomText" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Hello, World!" />
-
样式资源定义的格式,并寻找一个UI。样式可以应用于单个
视图
(从布局文件内)或整个活动
(从清单文件内)或应用程序。有关创建和应用样式的更多信息,请阅读 样式和主题。
注意:样式是使用在所提供的值引用的一个简单的资源
名称
属性(未XML文件的名称)。因此,你可以风格的资源与其他资源的简单一个XML文件中结合起来,在一个<资源>
元素。-
文件位置:
-
RES /价值/ 文件名 的.xml
的文件名 是任意的。元素的名字
将被用作资源ID。
资源引用:
-
在XML:
@ [包:]风格/ STYLE_NAME
句法:
-
<?xml的version = "1.0" encoding = "utf-8" ?> < resources > < style name = " style_name " parent = "@[package:]style/ style_to_inherit " > < item name = " [package:]style_property_name " > style_value </ item > </style> </resources>
内容:
- 例:
-
-
对于风格的XML文件(保存在
-
<?XML版本= “1.0” 编码= “UTF-8” ?> <资源> <样式 名称= “CustomText” 父= “@风格/文本” > < 项目名称= “机器人:TEXTSIZE” > 20SP </ 项> < 项目名称= “机器人:文字颜色” >#008 </ 项目> </作风> </资源>
该样式应用到一个XML文件中
-
<?xml的 世界!“ />
RES /价值/
):的TextView
(保存在RES /布局/
): -
-
res/values/
):TextView
(saved inres/layout/
): -