1. Brief introduction
We know that using the. properties file in Java to configure resource files is generally used in Chinese message_zh_cn.properties and English Message_en_ Us.properties two configuration files, then implemented through a series of configurations, not detailed here, in Android is also internationalized, and more convenient, because Android itself takes the XML resource file to manage all the string messages, as long as the messages are provided in different languages, The country corresponds to the content can be.
2, the preparation of resource files
Like the properties file in Java, you can also configure such a file on Android, except that Android is writing a string message in the Res/values directory, in order to give these messages a different language, country version, Developers need to add a few different language countries in the values directory, and the different values folders are named:
values-Language code-R Country code
Example: VALUES-ZH-RCN (Chinese) and Values-en-rus (English)
If you need to make the picture also internationalization, but also to add several different languages for the drawable directory version of the country, the different drawable folder is named: drawable-language code-R Country code
Build a Strings.xml in the Values-zh-rcn folder to represent Chinese resources
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "OK">Are you sure</string> <stringname= "Cancel">Cancel</string> <stringname= "MSG">Hello, little robot!</string></Resources>
Build a Strings.xml in the Valuesen-rus folder to represent English resources
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "OK">Ok</string> <stringname= "Cancel">Cancel</string> <stringname= "MSG">hello,android!</string></Resources>
3. Use these string resources in the layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" android:text= "@string/msg" /> <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content" android:text= "@string/ok" /> <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:text= "@string/cancel"/></LinearLayout>
Then load the layout file in the activity via Setcontentview (R.layout.main)
Operating effect:
First shows the English version, the Android simulator default in English, and then we can in Setting/language&input/language this will be the Android language set to ask Chinese, you can see the Chinese version.
Android Internationalization operations