使用createConfigurationContext()无法在运行时更改应用语言
解决Android运行时切换应用语言失败的问题
我看到你尝试通过代码在运行时切换应用语言但没成功,先帮你排查下代码里的小问题,再给你完整的正确实现方案~
先修正你代码里的明显错误
你最后一行代码里的confi是拼写错误,应该是config,这会直接导致编译失败,先把这个修正:
private static Context updateResources(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Resources res = context.getResources(); Configuration config = new Configuration(res.getConfiguration()); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) { config.setLocale(locale); LocaleList.setDefault(new LocaleList(locale)); context = context.createConfigurationContext(config); // 这里把confi改成config } else { config.locale = locale; res.updateConfiguration(config, res.getDisplayMetrics()); } return context; }
完整的运行时切换语言实现方案
仅仅修正拼写可能还不够,因为切换语言后需要让Activity重启才能生效,同时还要处理Application层面的配置,确保全局语言一致。
1. 完善语言更新方法
下面是覆盖所有主流Android版本的完整方法:
public static void changeAppLanguage(Context context, String languageCode) { Locale targetLocale = new Locale(languageCode); Locale.setDefault(targetLocale); Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { configuration.setLocale(targetLocale); configuration.setLocales(new LocaleList(targetLocale)); context = context.createConfigurationContext(configuration); } else { configuration.locale = targetLocale; resources.updateConfiguration(configuration, resources.getDisplayMetrics()); } // 更新Application的Context配置,确保全局语言一致 if (context.getApplicationContext() != null) { Resources appResources = context.getApplicationContext().getResources(); Configuration appConfig = appResources.getConfiguration(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { appConfig.setLocale(targetLocale); appConfig.setLocales(new LocaleList(targetLocale)); context.getApplicationContext().createConfigurationContext(appConfig); } else { appConfig.locale = targetLocale; appResources.updateConfiguration(appConfig, appResources.getDisplayMetrics()); } } }
2. 调用方法并重启Activity
在你触发语言切换的地方(比如按钮点击事件),调用上面的方法,然后重启当前Activity让语言生效:
// 示例:切换到英文 changeAppLanguage(this, "en"); // 重启当前Activity使语言设置生效 Intent intent = getIntent(); finish(); startActivity(intent); // 如果是在Fragment中,用getActivity()代替this
3. 保存用户选择的语言(可选但推荐)
为了让用户下次打开应用时保持选择的语言,你需要把语言代码保存到SharedPreferences中,然后在Application的onCreate()或者启动页的onCreate()中调用语言切换方法:
// 保存语言到SharedPreferences SharedPreferences sp = getSharedPreferences("AppSettings", MODE_PRIVATE); sp.edit().putString("selected_language", "en").apply(); // 在Application onCreate中读取并应用 @Override public void onCreate() { super.onCreate(); SharedPreferences sp = getSharedPreferences("AppSettings", MODE_PRIVATE); String language = sp.getString("selected_language", "zh"); // 默认中文 changeAppLanguage(this, language); }
额外注意事项
- 对于Android 12及以上,有些设备可能需要额外处理
configuration.setLocales(),确保LocaleList只包含目标语言 - 如果你的应用有多个Activity,最好在BaseActivity的
attachBaseContext()方法中更新Context配置,避免个别页面语言不生效:
@Override protected void attachBaseContext(Context newBase) { SharedPreferences sp = newBase.getSharedPreferences("AppSettings", MODE_PRIVATE); String language = sp.getString("selected_language", "zh"); Context updatedContext = updateResources(newBase, language); super.attachBaseContext(updatedContext); }
内容的提问来源于stack exchange,提问作者Helo Mali
相关产品推荐
相关产品推荐

