- 语法:
<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 是分配给该项的值。系统会使用
Bundle.getInt()
方法从元数据Bundle
中检索 ID。 android:value
- 分配给该项的值。下表列出了可以指定为值的数据类型以及组件可以用来检索这些值的
Bundle
方法:
类型 Bundle 方法 字符串:使用双反斜杠 ( \\
) 转义字符,例如\\n
表示新行,\\uxxxxx
表示 Unicode 字符getString()
整数:例如 100
getInt()
布尔值: true
或false
getBoolean()
颜色:采用 #rgb
、#argb
、#rrggbb
或#aarrggbb
格式getInt()
浮点数:例如 1.23
getFloat()
- 引入于:
- API 级别 1
<元数据>
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):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"]]