Android应用程序的国际化还是做得不错的,通过设置资源文件的名称,如:values-zh-rCN,系统就会自动根据当前系统设置的语言来设置,默认是选择values目录下的资源文件的。那么如果某个应用程序要指定默认语言,无论系统设置的语言是什么,都要显示默认语言,那要怎么做呢?其实很简单,只需要3步就可以了。
第一步,写一个类(FunctionApplication)继承Application,在onCreate()方法中实现如下方法:
1
2
3
4
5
6
7
8
9
10
11
|
@Override
public
void
onCreate() {
super
.onCreate();
String languageToLoad =
"zh"
;
Locale locale =
new
Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = getResources().getConfiguration();
DisplayMetrics metrics = getResources().getDisplayMetrics();
config.locale = Locale.SIMPLIFIED_CHINESE;
getResources().updateConfiguration(config, metrics);
}
|
第二步,将默认的AndroidManifest.xmlApplication设置为如下:
1
2
3
4
5
|
<
application
android:name
=
".FunctionApplication"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:configChanges
=
"locale"
>
|
其中android:configChanges="locale"是关键。当然如果你不使整个应用程序都编程中文,那么不用继承Application,直接在继承Activity的类中添加第一步的方法即可。当然别忘记在Activity标签中添加android:configChanges="locale"。
第三步,添加相应权限:
1
|
<
uses-permission
android:name
=
"android.permission.CHANGE_CONFIGURATION"
/>
|