Create Locale Directories and String Files
要添加对更多语言的支持,请在res /中创建其他值目录,其中包含连字符和目录名称末尾的ISO语言代码。 例如,values-es /是包含语言代码为“es”的语言环境简单资源的目录。 Android在运行时根据设备的区域设置加载适当的资源。
一旦决定了您将支持的语言,请创建资源子目录和字符串资源文件。 例如:
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
将每个区域设置的字符串值添加到相应的文件中。
在运行时,Android系统根据当前为用户设备设置的区域设置使用适当的字符串资源集。
例如,以下是针对不同语言的一些不同的字符串资源文件
在运行时,Android系统根据当前为用户设备设置的区域设置使用适当的字符串资源集。
例如,以下是针对不同语言的一些不同的字符串资源文件
English (default locale),
/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>
Spanish,
/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>
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>
Use the String Resources
您可以使用由<string>元素的name属性定义的资源名称在源代码和其他XML文件中引用字符串资源。在源代码中,可以使用语法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" />
参考网页:http://androiddoc.qiniudn.com/training/basics/supporting-devices/languages.html#UseString