轉載請註明來源: http://blog.csdn.net/kjunchen/article/details/51312995
Android 應用開發支持不同的語言
創建區域目錄和字符串文件
要支持更多的語言,在 res/ 下創建額外的 values 目錄以一個連字符 ‘-’ 和 ISO 語言代碼結尾命名。例如, values-es 目錄包含了一些簡單的資源,區域語言代碼爲 “es” 。Android設備在運行時,會根據語言環境的設置加載相應的資源。
一旦你決定支持某種語言,需要創建資源子目錄和字符串資源文件。如:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
爲每一種語言添加字符串值到相應的文件中。
在運行時,Android系統會根據用戶設備當前的語言設置加載相應的字符串資源文件。
例如,下面是一些不同語言的字符串資源文件。
英語(默認語言), /values/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>
西班牙語, /values-es/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
法語, /values-fr/strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mon Application</string>
<string name="hello_world">Bonjour le monde !</string>
</resources>
使用字符串資源
我們可以在源代碼和其他的XML文件中通過 < string > 元素中的 name 屬性來引用字符串資源。
在源代碼中可以使用 R.string.< string_name > 語法來引用資源。大多數方法都接受這種方式引用字符串資源。
例如:
// Get a string resource from your app's Resources
String hello = getResources().getString(R.string.hello_world);
// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);
在其他的 XML 文件中,只要 XML 屬性接受字符串值時都可以使用 @string/< string_name > 語法來引用字符串資源。
例如:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Android 多語言文件夾
常見語言文件夾如下:
- values-zh 中文
- values-zh-rCN 中文(中國)
- values-zh-rHK 中文(香港)
- values-en 英文
- values-en-rUS 英文(美國)
- values-en-rGB 英文(英國)
- values-de 德文
- values-fr 法文
- values-ja 日文
- values-ko 韓文
- values-es 西班牙文
- values-it 意大利文
- values-ar 阿拉伯文
如有問題歡迎加qq羣討論學習:365532949
HomePage:http://junkchen.com