摘要:該文章將向大家分享Android中的資源與國際化的問題。
今天給大家分享一下Android中的資源與國際化的問題,通常我們建立一個Android工程,目錄結構如所示:
我們主要看一下layout與values目錄,layout裡的xml檔案的我們應用使用布局的檔案,values裡的sring.xml是用來存放文字資源,一個key對應一個value值。
但是在實際應用開發中,通常橫屏(land)與豎屏(port)可能布局檔案有所不同,這時候我們可以獨自訂橫屏與豎屏的布局檔案(檔案名稱字要一樣),預設情況是載入layout目錄裡的布局檔案。同樣應用還要支援不同的語言,如果我們應用裡沒有定義手機所用語言的資源時,會預設載入values的值。
為了方便大家理解下面做個簡單的Demo.具體步驟如下:
第一步:建立一個Android工程,命名為ResourceDemo。
我們看一下layout目錄下的自動產生的main.xml布局檔案,代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
其中我們程式顯示的常值內容是在values/string.xml檔案中對應的hello的值,代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ResourceDemo!</string> <string name="app_name">ResourceDemo</string> </resources>
運行效果如所示:
port(豎屏模式)效果:
Ctrl+F11快速鍵模擬器變成橫屏(land)模式:
第二步:我們定義land與port模式的布局檔案,即在res/目錄下建立layout-land與layout-port兩個檔案夾,目錄結果如下所示:
layout-land目錄下main.xml和layout內容基本一樣,只是顯示內容不同,代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/land" /> </LinearLayout>
同理layou-port目錄下main.xml代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/port" /> </LinearLayout>
當然我們顯示的內容是在values/string.xml檔案裡定義的,這裡加了兩個值,代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ResourceDemo!</string> <string name="app_name">ResourceDemo</string> <string name="land">This is land mode.</string> <string name="port">This is port mode.</string> </resources>
然後再次運行程式,效果如下:
豎屏(port)效果:
橫屏(land)下效果:
通過上面例子可以得出如果我們已經定義了橫屏與豎屏布局檔案時,就不會在載入layout裡的同名布局檔案。
下面我們來講點國際化,通常國際化我們只要在res/目錄下在重新定義values-國家編號,如values-zh-rCN簡體漢語,values-zh-rTW繁體,values-jp日語等。
目錄結構如所示:
這裡我只在values-zh-rCN作了改動,代碼如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, ResourceDemo!</string> <string name="app_name">ResourceDemo</string> <string name="land">這是橫屏模式.</string> <string name="port">這是豎屏模式.</string> </resources>
我們運行程式之前,把手機語言改成中文,在settings(設定)->language & keyboards(語言與鍵盤)目錄下,選擇簡體中文,如所示:
最然在運行上述工程,效果如下:
這時候我們應用的顯示內容就為中文了,而不去顯示values/strings.xml裡的內容。
Ok~今天就到這裡,希望對大家特別是初學者有點協助.
時間:2014-05-07 08:52 來源:http://www.codesocang.com/jiaocheng/shoujikaifa/2014/0507/7474.html 瀏覽:6 次