android資源別名

來源:互聯網
上載者:User

當你要給android的多種裝置開發時, 要準備在多個configuration 下的資源,當然還要提供預設源. 如果在多個configuration下的資源是一樣時, 你可以把這相同的資源拷貝到相應的目錄下, 不過這不是一個好的實踐, 實際上我們可以使用資源別名.

不過該方法不適用於animation,  menu, raw, 和xml目錄下的資源.

如有個icon.png圖片, res/drawable-en-rCA/, res/drawable-fr-rCA/ 兩種configuration要適應, 它們的icon.png圖片是一樣的. 這時你可以把icon.png改個名字如 icon_alais.png放到res/drawable目錄下, 然後在 res/drawable-en-rCA/, res/drawable-fr-rCA/ 目錄下用icon.xml檔案對這個icon_alais.png進行引用. 在產生的R類裡, 我們用R.drawable.icon這個變數進行引用

Drawable
用<bitmap> element 進行資源的引用
Xml代碼 
<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@drawable/icon_ca" /> 


Layout
使用<include>和<merge>元素
Xml代碼 
<?xml version="1.0" encoding="utf-8"?><merge>    <include layout="@layout/main_ltr"/></merge> 


Strings and other simple values
用資源id做為引用
Xml代碼 
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello</string>    <string name="hi">@string/hello</string></resources> 


Xml代碼 
<?xml version="1.0" encoding="utf-8"?><resources>    <color name="yellow">#f00</color>    <color name="highlight">@color/red</color></resources> 

本文出自“wen66”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.