在Android应用程序中更改语言
问题描述:
如果需要在用户需要时更改应用程序的语言,我正在编写应用程序。不同语言的数据存储在数据库中,从中获取数据并更新UI。我想如果设备不支持特定的语言字体,应该怎么做。任何帮助将不胜感激。提前感谢。 _/| _在Android应用程序中更改语言
答
我不知道更多关于这一点,但...
例如,如果你希望你的应用程序同时支持英语 和法国的字符串(除了默认的字符串), 你可以简单地创建两个额外的 资源目录,称为/res/values-en
(英文strings.xml)和 /res/values-fr
(用于法文strings.xml)。
在strings.xml
文件中, 资源名称相同。
例如,/res/values-en/strings.xml
文件可能 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
鉴于,/res/values-fr/strings.xml文件应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Bonjour en Français!</string>
</resources>
一显示字符串的/ res/layout目录中的默认布局文件通过变量名@ string/hello引用 字符串,而不考虑字符串资源所在的语言或目录 。
的Android操作系统确定字符串(法语,英语,或默认)在runtime.A布局一个TextView控制 加载其中 版本显示的字符串可能是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" >
</LinearLayout>
该字符串以正常方式以编程方式访问:
String str = getString(R.string.hello);
就这么简单。
更多你会发现Here