- 語法:
<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
屬性會將資源的數值 ID 指派到「zoo
」,而非儲存在資源中的值:<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()
方法從中繼資料套件中擷取 ID。 android:value
- 指派給項目的值。下表列出了可以指派為值的資料類型,以及元件用來擷取這些值的套件方法:
類型 套件方法 字串值,使用雙反斜線 ( \\
) 來逸出字元,例如用於 Unicode 字元的「\\n
」和「\\uxxxxx
」。getString()
整數值,例如「 100
」getInt()
布林值,「 true
」或「false
」getBoolean()
色彩值,格式為「 #rgb
」、「#argb
」、「#rrggbb
」或「#aarrggbb
」getInt()
浮點值,例如「 1.23
」getFloat()
- 導入版本:
- API 級別 1
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2022-05-06 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"Translation issue"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]