private static void updateLanguage(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration config = resources.getConfiguration();
Locale contextLocale = config.locale;
if (equals(contextLocale.getLanguage(), locale.getLanguage())
&& equals(contextLocale.getCountry(), locale.getCountry())) {
return;
}
DisplayMetrics dm = resources.getDisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
config.locale = locale;
}
resources.updateConfiguration(config, dm);
}
那么如果是应用内切换语言呢?我们可以仿照系统切换语言的方式,把我们自己所有的 Activity 全关掉,然后启动首页的 Activity 即可,在打开的 Activity#onCreate 中把 Activity 和 Application 的 Locale 都设置为我们设置的语言即可,当然ÿ