Supporting Different devices - Supporting Different Languages
2015-02-27 09:39
232 查看
It’s always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory in each Android project.
If you created your project using the Android SDK Tools (readCreating
an Android Project), the tools create a
are subdirectories for various resource types. There are also a few default files such as
To add support for more languages, create additional
include a hyphen and the ISO language code at the end of the directory name. For example,
"es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see Providing
Alternative Resources.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
English (default locale),
Spanish,
French,
Note: You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see Localization.
You can reference your string resources in your source code and other XML files using the resource name defined by the
In your source code, you can refer to a string resource with the syntax
a string resource this way.
For example:
String hello =
// Or supply a string resource to a method that requires a string
TextView textView = new TextView(this);
textView.setText(R.string.hello_world);[/code]
In other XML files, you can refer to a string resource with the syntax
value.
For example:
NEXT: SUPPORTING
DIFFERENT SCREENS
If you created your project using the Android SDK Tools (readCreating
an Android Project), the tools create a
res/directory in the top level of the project. Within this
res/directory
are subdirectories for various resource types. There are also a few default files such as
res/values/strings.xml, which holds your string values.
Create Locale Directories and String Files
To add support for more languages, create additional valuesdirectories inside
res/that
include a hyphen and the ISO language code at the end of the directory name. For example,
values-es/is the directory containing simple resourcess for the Locales with the language code
"es". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see Providing
Alternative Resources.
Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:
MyProject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For example, the following are some different string resource files for different languages.
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>
Note: You can use the locale qualifier (or any configuration qualifer) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see Localization.
Use the String Resources
You can reference your string resources in your source code and other XML files using the resource name defined by the <string>element's
nameattribute.
In your source code, you can refer to a string resource with the syntax
R.string.<string_name>. There are a variety of methods that accept
a string resource this way.
For example:
// Get a string resource from your app's [code]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);[/code]
In other XML files, you can refer to a string resource with the syntax
@string/<string_name>whenever the XML attribute accepts a string
value.
For example:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
NEXT: SUPPORTING
DIFFERENT SCREENS
相关文章推荐
- android-Supporting Different Devices
- 阅读Android官方教程 Google Training 1.2 ------ Supporting Different Devices
- Andriod开发学习-Supporting Different Languages
- Supporting Different Devices - Supporting Different Screens
- Android Develop Training——Android应用支持多设备(Supporting Different Devices)
- Supporting Different Devices笔记
- Supporting Different Devices - Supporting Different Platform Versions
- Supporting Different Languages
- Android doc |Getting Started|部分 部分译文 --Supporting Different Devices
- 《Training:Supporting Different Languages》
- lesson 2 Supporting Different Devices
- Android学习记录三(Supporting Different Devices)
- (二)Supporting Different Devices支持不同的设备
- Supporting Different Screens
- android-Supporting Different Screens in Web Apps
- (3)Supporting different device
- 支持不同屏幕(Supporting Different Screens)
- Tensorflow--GPU/CPU/ contain variables in different devices
- String tables make it easy to localize your application into different languages
- 支持不同密度(Supporting Different Densities)