android meta-data 使用詳解

來源:互聯網
上載者:User

<meta-data android:name="string"

           android:resource="resource specification"

           android:value="string" />

這是該元素的基本結構.可以包含在

<activity> <activity-alias> <service> <receiver>四個元素中。

這個名字值是額外的任意的可以提供給父組件的資料。一個組件元素能夠包含任意數量的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.getInt()來從meta-data中找到。

android:value

指定給這一項的值。可以作為值來指定的資料類型並且組件用來找回那些值的Bundle方法列在了下面的表中。

Type                                                               Bundle method

String value, using double backslashes (//) to                getString()

escape characters — such as"//n" and "//uxxxxx"

for a Unicode character.

Integer value, such as "100"                                    getInt()

Boolean value, either "true" or "false"                    getBoolean()

Color value, in the form "#rgb", "#argb",                     getString()

"#rrggbb", or "#aarrggbb"

Float value, such as "1.23"                                   getFloat()

獲得a meta-data 的值:

     <meta-data android:name="foo" android:value="@string/app_name"/>

Java代碼  
  1. ActivityInfo ai = activity.getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA);      
  2. String foo = ai.metaData.getString("foo"
    );   
  3. Toast.makeText(this

    "meta:"
    +foo, 
    1
    ).show();   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.