Tài nguyên kiểu
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tài nguyên kiểu sẽ xác định định dạng và tìm kiếm một giao diện người dùng.
Bạn có thể áp dụng một kiểu cho từng View
riêng lẻ (từ trong một tệp bố cục) hoặc cho toàn bộ Activity
hoặc ứng dụng (từ trong tệp kê khai).
Để biết thêm thông tin về cách tạo và áp dụng kiểu, vui lòng đọc bài viết về Kiểu và giao diện.
Lưu ý: Kiểu là một tài nguyên đơn giản được tham chiếu bằng cách sử dụng giá trị đã cung cấp trong thuộc tính name
(không phải tên của tệp XML). Do đó, bạn có thể kết hợp tài nguyên kiểu với các tài nguyên đơn giản khác trong một tệp XML, bên dưới một phần tử <resources>
.
- vị trí tệp:
res/values/filename.xml
Bạn có thể tuỳ ý đặt tên tệp. name
của phần tử này sẽ được dùng làm mã nhận dạng tài nguyên.
- mã tham chiếu tài nguyên:
-
Trong XML:
@[package:]style/style_name
- cú pháp:
-
<?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>
- phần tử:
-
<resources>
- Bắt buộc. Đây phải là nút gốc.
Không có thuộc tính nào.
<style>
- Xác định một kiểu duy nhất. Chứa phần tử
<item>
.
thuộc tính:
name
- Chuỗi. Bắt buộc. Tên cho kiểu được dùng làm mã nhận dạng tài nguyên để áp dụng kiểu cho thành phần hiển thị (View), Hoạt động (Activity) hoặc ứng dụng (application).
parent
- Tài nguyên kiểu. Tham chiếu đến một kiểu mà kiểu này sẽ kế thừa các thuộc tính kiểu.
<item>
- Xác định một thuộc tính duy nhất cho kiểu. Phải là phần tử con của phần tử
<style>
.
thuộc tính:
name
- Tài nguyên về thuộc tính. Bắt buộc. Tên của thuộc tính kiểu sẽ được xác định, với tiền tố gói nếu cần (ví dụ:
android:textColor
).
- ví dụ:
-
- Tệp XML cho kiểu (được lưu trong
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>
- Tệp XML áp dụng kiểu cho
TextView
(được lưu trong 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!" />
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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 ```"]]