android國際化操作

來源:互聯網
上載者:User

標籤:

1、簡單介紹

我們知道在java中通過.properties檔案來配置資源檔,一般用的有中文message_zh_CN.properties和英文message_en_US.properties兩個設定檔,然後通過一系列的配置來實現,這裡不詳細介紹,在android也有國際化,而且更方便,因為android本身採用了Xml資源檔來管理所有的字串訊息,只要為各訊息提供不同語言、國家對應的內容即可。

2、資源檔的編寫

和java中的properties檔案一樣,在android也要配置這樣的檔案,只不過android是在res/values目錄下編寫字串訊息,為了給這些訊息提供不同的語言、國家的版本,開發人員需要在values目錄下添加幾個不同語言國家版本,不同values檔案夾的命名方式為:

  values-語言代碼-r國家代碼

例如:values-zh-rCN(中文)和values-en-rUS(英文)

如果需要讓圖片也實現國際化,還要為drawable目錄添加幾個不同語言國家的版本,不同drawable檔案夾的命名方式為:drawable-語言代碼-r國家代碼

在values-zh-rCN檔案夾下建一個strings.xml代表中文資源

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="ok">確定</string>    <string name="cancel">取消</string>    <string name="msg">你好啊,小機器人!</string></resources>

在valuesen-rUS檔案夾下建一個strings.xml代表英文資源

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="ok">ok</string>    <string name="cancel">cancel</string>    <string name="msg">Hello,Android!</string></resources>
3、在布局檔案中使用這些字串資源
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/msg"        />        <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/ok"        />        <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/cancel"/></LinearLayout>

接著在activity中通過setContentView(R.layout.main)載入布局檔案

運行效果:

首先顯示出來的是英文版的,android模擬器預設英文的,接著我們可以在Setting/Language&Input/Language這將android語言設定問中文的,就可以看到中文版的。

 

android國際化操作

聯繫我們

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