支持不同的语言
2015-12-01 10:43
253 查看
创建对应语言的资源文件,例如values-es/就是包含“es”的语言代码。
English(默认的语言),/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>Franch,/values-es/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>可以自定义默认的语言。
在java代码中使用strings的方法:
// 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中使用:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
MyProject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
English(默认的语言),/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>Franch,/values-es/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>可以自定义默认的语言。
在java代码中使用strings的方法:
// 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中使用:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
相关文章推荐
- android gradle compile(一)
- 深圳四条高速路将取消收费 拖车每月省近2000
- C语言求最小公倍数和最大公约数三种算法
- Raspberry Pi B+ 实验
- 响铃:互联网金融理财端寡头时代来临,到底谁该被剩下来?
- Java快速教程
- 如何将GPS数据转换成KML数据?使用JAK的例子之二
- 第六周实践项目3—括号的匹配
- git 修复 bug 的基本流程
- C#_Assembly-Reflection_程序集与反射 练习<一> 记事本插件编写
- 每天一个linux命令(15):whereis 命令
- Android的Canvas、Bitmap、Drawable和Paint
- PHP下载txt文件到浏览器
- android 设置系统屏幕亮度
- 广电计量检测基地建设情况(2015年)
- java集合框架总结
- 前端面试题目搜集
- IQKeyboardManager
- struct和typedef struct彻底明白了
- MTK android Driver:UART介绍