《Training:Supporting Different Languages》
来源:互联网 发布:日历 节假日 农历 js 编辑:程序博客网 时间:2024/06/05 05:49
注:本人非专业翻译人员,编写此博客只是为了自学使用,如有疑问,请参考官方文档。
从您的应用程序代码中提取UI字符串并将其保存在外部文件中是一个很好的做法。 Android使每个在Android项目中的资源目录轻松实现。
如果您使用Android SDK工具(请参阅创建Android项目)创建了项目,那么这些工具会在项目的顶层创建一个res /目录。 在这个res /目录中有各种资源类型的子目录。 还有一些默认文件,如res / values / strings.xml,其中包含您的字符串值。
Create Locale Directories and String Files
要添加对更多语言的支持,请在res /中创建附加值目录,其中包含连字符和目录名末尾的ISO语言代码。 例如,值-es /是包含语言代码为“es”的区域设置的简单资源的目录。 Android将在运行时根据设备的区域设置加载相应的资源。 有关更多信息,请参阅Providing Alternative Resources。
一旦您决定了将支持的语言,请创建资源子目录和字符串资源文件。 例如:
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>
Use the String Resources
您可以使用由<string>元素的name属性定义的资源名称来引用源代码和其他XML文件中的字符串资源。
在您的源代码中,您可以使用语法R.string.<string_name>引用字符串资源。 有多种方法可以通过这种方式接受字符串资源。
例如:
// Get a string resource from your app's ResourcesString hello = getResources().getString(R.string.hello_world);// Or supply a string resource to a method that requires a stringTextView 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" />
- 《Training:Supporting Different Languages》
- Supporting Different devices - Supporting Different Languages
- 《Training:Supporting Different Screens》
- android training Supporting Different Screens
- 《Training:Supporting Different Platform Versions》
- Android Training--Supporting Different Screen Sizes
- Android官方文档翻译 十三 3.1Supporting Different Languages
- 阅读Android官方教程 Google Training 1.2 ------ Supporting Different Devices
- Supporting Different Devices - Supporting Different Screens
- Supporting Different Devices - Supporting Different Platform Versions
- (3)Supporting different device
- Supporting Different Screens
- Supporting Different Screens
- Supporting Different Screens
- dp-Supporting Different Screens
- Supporting Different Screen Sizes
- android-Supporting Different Devices
- Supporting Different Devices笔记
- React Native之项目打包
- JAVAEMAIL发送邮件
- 位置无关码、位置有关码、链接地址、加载地址
- day33_hibernate一级缓存
- vb.net 教程 5-14 图像处理之内存处理基础3
- 《Training:Supporting Different Languages》
- Spring Mybatis jUnit 单元测试初体验
- Kafka的web 监控平台
- 二叉树先序非递归创建及先序中序后序非递归遍历
- java基础问题
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 向URL末尾添加查询字符串参数的辅助函数及获取Url的参数函数
- jdk源码剖析之EnumMap
- 41.top10热门品类之获取top10品类并写入MySQL