- 語法:
<meta-data android:name="string" android:resource="resource specification" android:value="string" />
- 包含於:
<activity>
<activity-alias>
<application>
<provider>
<receiver>
<service>
- 說明:
- 適用於其他任意資料項目的名稱/值配對,
提供給父項元件元件元素可包含
<meta-data>
個子元素數量。這些屬性的值都在單一Bundle
物件中收集,並以PackageItemInfo.metaData
欄位的形式提供給元件使用。透過
value
指定一般值 屬性。如要將資源 ID 指派為值,請使用resource
屬性。例如: 下列程式碼會指派@string/kangaroo
中儲存的任何值 設為zoo
名稱的資源:<meta-data android:name="zoo" android:value="@string/kangaroo" />
另一方面,使用
resource
屬性會指派zoo
資源的數值 ID,而非儲存在資源中的值:<meta-data android:name="zoo" android:resource="@string/kangaroo" />
強烈建議您避免為 多個獨立的
<meta-data>
項目。相反地 將複雜的資料與元件建立關聯、儲存為資源 使用resource
屬性指出其 ID 的元件。 - 屬性:
android:name
- 項目的專屬名稱。如要保持名稱不重複,請使用
Java 式命名慣例,例如
「
com.example.project.activity.fred
」。 android:resource
- 資源參照。資源 ID 是系統指派的值
新增至該物件這個 ID 是從中繼資料
Bundle
中擷取而來,Bundle.getInt()
方法。 android:value
- 指派給項目的值。可以指派為值的資料類型,
下表列出元件用來擷取這些值的
Bundle
方法:
類型 Bundle 方法 字串:使用雙反斜線 ( \\
) 來逸出字元,例如\\n
用於換行,\\uxxxxx
則代表萬國碼 (Unicode) 字元getString()
整數:例如 100
getInt()
布林值: true
或false
getBoolean()
顏色:格式為 #rgb
、#argb
、#rrggbb
或#aarrggbb
getInt()
浮點值:例如 1.23
getFloat()
- 導入版本:
- API 級別 1
<meta-data>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],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"]]