- ไวยากรณ์:
<meta-data android:name="string" android:resource="resource specification" android:value="string" />
- ที่อยู่ใน:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
<service>
- description:
- คู่ชื่อ-ค่าของรายการข้อมูลเพิ่มเติมที่กำหนดเองที่สามารถ
ที่ส่งไปยังคอมโพเนนต์ระดับบนสุด องค์ประกอบคอมโพเนนต์อาจมี
จำนวนองค์ประกอบย่อย
<meta-data>
รายการ ค่าจากทั้งหมด จะถูกรวบรวมไว้ในออบเจ็กต์Bundle
เดียวและทำให้ พร้อมใช้งานสำหรับคอมโพเนนต์เป็นPackageItemInfo.metaData
ระบุค่าทั่วไปผ่าน
value
หากต้องการกำหนดรหัสทรัพยากรเป็นค่า ให้ใช้พารามิเตอร์resource
แทน ตัวอย่างเช่น โค้ดต่อไปนี้จะกำหนดค่าใดๆ ที่จัดเก็บใน@string/kangaroo
ทรัพยากรของชื่อzoo
:<meta-data android:name="zoo" android:value="@string/kangaroo" />
ในทางกลับกัน การใช้แอตทริบิวต์
resource
จะกำหนดzoo
รหัสตัวเลขของทรัพยากร ไม่ใช่ค่าที่เก็บไว้ในทรัพยากร<meta-data android:name="zoo" android:resource="@string/kangaroo" />
เราขอแนะนำอย่างยิ่งให้คุณหลีกเลี่ยงการใส่ข้อมูลที่เกี่ยวข้อง รายการ
<meta-data>
หลายรายการแยกกัน แต่ถ้าคุณ มีข้อมูลที่ซับซ้อนที่จะเชื่อมโยงกับคอมโพเนนต์ จัดเก็บเป็นทรัพยากร และ ใช้แอตทริบิวต์resource
เพื่อแจ้งรหัสคอมโพเนนต์ - ดังนี้
android:name
- ชื่อที่ไม่ซ้ำกันสำหรับรายการ หากต้องการให้ชื่อไม่ซ้ำกัน ให้ใช้
แบบแผนการตั้งชื่อแบบ Java เช่น
"
com.example.project.activity.fred
" android:resource
- การอ้างอิงแหล่งข้อมูล รหัสของทรัพยากรคือค่าที่กำหนดให้
ไปยังรายการ รหัสนี้จะดึงมาจากข้อมูลเมตา
Bundle
โดยใช้Bundle.getInt()
วิธี android:value
- มูลค่าที่กำหนดให้กับรายการ ประเภทข้อมูลที่สามารถกำหนดเป็นค่าและ
เมธอด
Bundle
ที่คอมโพเนนต์ใช้เพื่อเรียกข้อมูลค่าเหล่านั้นจะแสดงในตารางต่อไปนี้ วันที่
ประเภท วิธีการรวมกลุ่ม สตริง: ใช้เครื่องหมายแบ็กสแลชคู่ ( \\
) เพื่อหลีกอักขระ เช่น\\n
สำหรับบรรทัดใหม่ และ\\uxxxxx
สำหรับอักขระ UnicodegetString()
จำนวนเต็ม เช่น 100
getInt()
บูลีน: true
หรือfalse
getBoolean()
สี: ในรูปแบบ #rgb
,#argb
,#rrggbb
หรือ#aarrggbb
getInt()
ลอย: เช่น 1.23
getFloat()
- เปิดตัวใน
- API ระดับ 1
<meta-data>
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา 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,["# <meta-data\u003e\n\nsyntax:\n:\n\n ```xml\n \u003cmeta-data android:name=\"string\"\n android:resource=\"resource specification\"\n android:value=\"string\" /\u003e\n ```\n\ncontained in:\n: [\u003cactivity\u003e](/guide/topics/manifest/activity-element)\n\n [\u003cactivity-alias\u003e](/guide/topics/manifest/activity-alias-element)\n\n [\u003capplication\u003e](/guide/topics/manifest/application-element)\n\n [\u003cprovider\u003e](/guide/topics/manifest/provider-element)\n\n [\u003creceiver\u003e](/guide/topics/manifest/receiver-element)\n\n [\u003cservice\u003e](/guide/topics/manifest/service-element)\n\ndescription:\n: A name-value pair for an item of additional, arbitrary data that can be\n supplied to the parent component. A component element can contain any\n number of `\u003cmeta-data\u003e` subelements. The values from all of\n them are collected in a single [Bundle](/reference/android/os/Bundle) object and made\n available to the component as the\n [PackageItemInfo.metaData](/reference/android/content/pm/PackageItemInfo#metaData) field.\n\n Specify ordinary values through the [value](/guide/topics/manifest/meta-data-element#val)\n attribute. To assign a resource ID as the value, use the\n [resource](/guide/topics/manifest/meta-data-element#rsrc) attribute instead. For example,\n the following code assigns whatever value is stored in the `@string/kangaroo`\n resource to the `zoo` name:\n\n\n ```xml\n \u003cmeta-data android:name=\"zoo\" android:value=\"@string/kangaroo\" /\u003e\n ```\n\n\n On the other hand, using the `resource` attribute assigns `zoo`\n the numeric ID of the resource, not the value stored in the resource:\n\n\n ```xml\n \u003cmeta-data android:name=\"zoo\" android:resource=\"@string/kangaroo\" /\u003e\n ```\n\n\n We highly recommend that you avoid supplying related data as\n multiple separate `\u003cmeta-data\u003e` entries. Instead, if you\n have complex data to associate with a component, store it as a resource and\n use the `resource` attribute to inform the component of its ID.\n\nattributes:\n:\n\n `android:name`\n : A unique name for the item. To keep the name unique, use a\n Java-style naming convention, such as\n \"`com.example.project.activity.fred`\".\n\n `android:resource`\n : A reference to a resource. The ID of the resource is the value assigned\n to the item. The ID is retrieved from the meta-data `Bundle` using the\n [Bundle.getInt()](/reference/android/os/BaseBundle#getInt(java.lang.String)) method.\n\n `android:value`\n : The value assigned to the item. The data types that can be assigned as values and the\n `Bundle` methods that components use to retrieve those values are listed in the following table:\n\n\n | Type | Bundle method |\n |---------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|\n | String: use double backslashes (`\\\\`) to escape characters, such as `\\\\n` for a new line and `\\\\uxxxxx` for a Unicode character | [getString()](/reference/android/os/BaseBundle#getString(java.lang.String)) |\n | Integer: for example, `100` | [getInt()](/reference/android/os/BaseBundle#getInt(java.lang.String)) |\n | Boolean: either `true` or `false` | [getBoolean()](/reference/android/os/BaseBundle#getBoolean(java.lang.String)) |\n | Color: in the form `#rgb`, `#argb`, `#rrggbb`, or `#aarrggbb` | [getInt()](/reference/android/os/BaseBundle#getInt(java.lang.String)) |\n | Float: for example, `1.23` | [getFloat()](/reference/android/os/Bundle#getFloat(java.lang.String)) |\n\n\nintroduced in:\n: API level 1"]]