爲了添加對更多語言的支持,就要在res/裏面另外再建立包含一個其路徑名稱的末尾帶上連字符後面,再跟上ISO語言編碼的 values 路徑 . 例如,values-es/ 是包含帶有語言編碼「es」的本地方言簡單資源的路徑 . Android 會根據設備在運行時的方言設置來加載相近的資源 . 更多信息,鍵 提供可選資源. html
一旦你已經決定了你所要支持的語言,那就要建立資源子路徑和字符串資源文件了. 例如 : java
MyProject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
將每個方言的字符串值都添加到相近的文件中. android
在運行時,Android系統會基於用戶設備的當前方言設置來使用相近的字符串資源集合 . app
例如,下面是一些用於不一樣語言的字符串資源文件 . ide
英語(default locale), /values/strings.xml: ui
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string> </resources>
西班牙語Spanish, /values-es/strings.xml: this
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="title">Mi Aplicación</string> <string name="hello_world">Hola Mundo!</string> </resources>
法語French, /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>
注意:你能夠在任何資源類型上使用到這個方言限定符 (或者任何配置限定符), 好比你想要提供位圖的方言化版本. 更多的信息,見 本地化. spa
在你的源代碼中,你能夠使用 R.string.<字符串_名稱>來引用字符串資源. 有各類方法能夠用這種方式接受一個字符串資源 . code
例如 :
// 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" />
來源:
http://developer.android.com/training/basics/supporting-devices/languages.html