แหล่งข้อมูลรูปแบบ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ทรัพยากรรูปแบบจะกำหนดรูปแบบและมองหา UI
รูปแบบสามารถใช้กับ View
แต่ละรายการ (จากภายในไฟล์เลย์เอาต์) หรือ
Activity
หรือแอปพลิเคชันทั้งหมด (จากภายในไฟล์ Manifest)
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการสร้างและการใช้รูปแบบ โปรดอ่าน
สไตล์และธีม
หมายเหตุ: รูปแบบเป็นแหล่งข้อมูลแบบง่ายๆ ที่มีการอ้างอิง
โดยใช้ค่าที่ระบุในแอตทริบิวต์ name
(ไม่ใช่ชื่อไฟล์ XML) อาส
คุณสามารถรวมทรัพยากรด้านรูปแบบเข้ากับทรัพยากรง่ายๆ อื่นๆ ในไฟล์ XML เดียว
ภายใต้องค์ประกอบ <resources>
1 รายการ
- ตำแหน่งที่ตั้งไฟล์:
res/values/filename.xml
-
ตั้งชื่อไฟล์ตามใจชอบ ระบบจะใช้
name
ขององค์ประกอบเป็นรหัสทรัพยากร
- การอ้างอิงทรัพยากร:
-
ใน 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
- สตริง ต้องระบุ ชื่อของรูปแบบ ซึ่งใช้เป็น
รหัสทรัพยากรที่จะนำรูปแบบไปใช้กับข้อมูลพร็อพเพอร์ตี้ กิจกรรม หรือแอปพลิเคชัน
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>
- ไฟล์ XML ที่นำรูปแบบไปใช้กับ
TextView
(บันทึกเมื่อ 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 และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 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 ```"]]