样式资源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
样式资源定义界面的格式和外观。样式可应用于单个 View
(从布局文件中)或应用于整个 Activity
或应用(从清单文件中)。
如需详细了解如何创建和应用样式,请参阅样式和主题。
注意:样式是使用 name
属性中提供的值(不是 XML 文件的名称)引用的简单资源。因此,您可以在一个 XML 文件中将样式资源与其他简单资源合并到一个 <resources>
元素下。
- 文件位置:
res/values/filename.xml
文件名可以任意设置。元素的 name
将用作资源 ID。
- 资源引用:
-
在 XML 中:
@[package:]style/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>
- 元素:
-
<resources>
- 必需。该元素必须是根节点。
无属性。
<style>
- 定义单个样式。包含
<item>
元素。
属性:
name
- 字符串。必需。样式的名称,用作将样式应用于 View、Activity 或应用的资源 ID。
parent
- 样式资源。对此样式应从中继承样式属性的样式的引用。
<item>
- 定义样式的单个属性。必须是
<style>
元素的子元素。
属性:
name
- 属性资源。必需。如要定义的样式属性的名称,必要时带有包前缀(例如
android:textColor
)。
- 示例:
-
- 样式的 XML 文件(保存在
res/values/
中):
-
<?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>
- 将样式应用于
TextView
的 XML 文件(保存在 res/layout/
中):
-
<?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!" />
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Style resource\n\nA style resource defines the format and look for a UI.\nA style can be applied to an individual [View](/reference/android/view/View) (from within a layout file) or to\nan entire [Activity](/reference/android/app/Activity) or application (from within the manifest file).\n\nFor more information about creating and applying styles, please read\n[Styles and Themes](/guide/topics/ui/themes).\n\n**Note:** A style is a simple resource that is referenced\nusing the value provided in the `name` attribute (not the name of the XML file). As\nsuch, you can combine style resources with other simple resources in the one XML file,\nunder one `\u003cresources\u003e` element.\n\nfile location:\n: `res/values/`*filename*`.xml` \n\n The filename is arbitrary. The element's `name` will be used as the resource ID.\n\nresource reference:\n:\n In XML: `@[package:]style/`*style_name*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstyle\n name=\"style_name\"\n parent=\"@[package:]style/style_to_inherit\"\u003e\n \u003citem\n name=\"[package:]style_property_name\"\n \u003estyle_value\u003c/item\u003e\n \u003c/style\u003e\n \u003c/resources\u003e\n ```\n\nelements:\n:\n\n `\u003cresources\u003e`\n : **Required.** This must be the root node.\n\n No attributes.\n\n `\u003cstyle\u003e`\n : Defines a single style. Contains `\u003citem\u003e` elements.\n\n attributes:\n\n `name`\n : *String* . **Required**. A name for the style, which is used as the\n resource ID to apply the style to a View, Activity, or application.\n\n `parent`\n : *Style resource*. Reference to a style from which this\n style should inherit style properties.\n\n\n `\u003citem\u003e`\n : Defines a single property for the style. Must be a child of a\n `\u003cstyle\u003e` element.\n\n \u003cbr /\u003e\n\n\n attributes:\n\n `name`\n : *Attribute resource* . **Required** . The name of the style property\n to be defined, with a package prefix if necessary (for example `android:textColor`).\n\n\nexample:\n:\n\n XML file for the style (saved in `res/values/`):\n :\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstyle name=\"CustomText\" parent=\"@style/Text\"\u003e\n \u003citem name=\"android:textSize\"\u003e20sp\u003c/item\u003e\n \u003citem name=\"android:textColor\"\u003e#008\u003c/item\u003e\n \u003c/style\u003e\n \u003c/resources\u003e\n ```\n\n XML file that applies the style to a [TextView](/reference/android/widget/TextView)\n (saved in `res/layout/`):\n :\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cEditText\n style=\"@style/CustomText\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"Hello, World!\" /\u003e\n ```"]]