第一步:
写一个类(FunctionApplication)继承Application,在onCreate()方法中实现如下方法:
@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);
}
其中android:configChanges="locale"是关键。当然如果你不使整个应用程序都编程中文,那么不用继承Application,直接在继承Activity的类中添加第一步的方法即可。当然别忘记在Activity标签中添加android:configChanges="locale"。
注:android:configChanges可以设置很多属性,具体属性如下:
AndroidManifest.xmlApplication设置为如下:
android:name=".FunctionApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:configChanges="locale" >
第三步,添加相应权限: