android

app内动态切换语言

2018-10-31  本文已影响0人  WLHere
  1. 设置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);
    }
  1. 重置语言相关静态属性
  • http header:"Accept-Language"
  • 其他属性
  1. 重启activity
activity.recreate();
  1. 完成
上一篇下一篇

猜你喜欢

热点阅读