app内动态切换语言
2018-10-31 本文已影响0人
WLHere
- 设置context的语言
public static void setApplicationLanguage(Context context, Locale locale) {
Resources resources = context.getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
context.getApplicationContext().createConfigurationContext(config);
Locale.setDefault(locale);
}
resources.updateConfiguration(config, dm);
}
- 重置语言相关静态属性
- http header:"Accept-Language"
- 其他属性
- 重启activity
activity.recreate();
- 完成