Android Studio全局设置Locale无效,仅MainActivity生效问题求助
解决Android中Application类切换Locale无效的问题
问题根源
- Application中更新全局资源配置后,已启动的Activity不会自动同步新配置,所以调用后当前页面无变化。
- 原代码仅适配Android 7.0以下版本,高版本系统(API 24+)需要使用新的Locale配置逻辑。
- 未持久化用户选择的语言,重启应用后会恢复为系统语言。
完整解决方案
1. 改造MyApplication的Locale配置逻辑
添加持久化存储保存用户语言选择,同时适配不同Android版本的Locale设置方式:
public class MyApplication extends Application { private static MyApplication instance; private SharedPreferences prefs; @Override public void onCreate() { super.onCreate(); instance = this; prefs = getSharedPreferences("AppSettings", MODE_PRIVATE); } public static MyApplication getInstance() { return instance; } public void setLocale(String languageCode) { // 持久化用户选择的语言 prefs.edit().putString("selected_language", languageCode).apply(); Locale locale = new Locale(languageCode); Locale.setDefault(locale); // 适配不同版本的配置更新 Resources resources = getResources(); Configuration config = resources.getConfiguration(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { config.setLocale(locale); config.setLocales(new LocaleList(locale)); createConfigurationContext(config); } else { config.setLocale(locale); resources.updateConfiguration(config, resources.getDisplayMetrics()); } // 重启应用使配置生效 Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } // 应用启动时加载保存的语言 @Override protected void attachBaseContext(Context base) { prefs = base.getSharedPreferences("AppSettings", MODE_PRIVATE); String languageCode = prefs.getString("selected_language", Locale.getDefault().getLanguage()); super.attachBaseContext(updateBaseContextLocale(base, languageCode)); } // 更新Context的Locale配置 private Context updateBaseContextLocale(Context context, String languageCode) { Locale locale = new Locale(languageCode); Locale.setDefault(locale); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return updateResourcesLocale(context, locale); } else { return updateResourcesLocaleLegacy(context, locale); } } // API 24+的Locale更新方式 @TargetApi(Build.VERSION_CODES.N) private Context updateResourcesLocale(Context context, Locale locale) { Configuration config = context.getConfiguration(); config.setLocale(locale); config.setLocales(new LocaleList(locale)); return context.createConfigurationContext(config); } // API 24以下的Locale更新方式 @SuppressWarnings("deprecation") private Context updateResourcesLocaleLegacy(Context context, Locale locale) { Resources resources = context.getResources(); Configuration config = resources.getConfiguration(); config.setLocale(locale); resources.updateConfiguration(config, resources.getDisplayMetrics()); return context; } }
2. 调用方式
在设置页面或任意位置调用切换语言:
MyApplication.getInstance().setLocale("en");
3. 关键注意事项
- 必须重启Activity/应用:已启动的Activity资源配置不会自动刷新,代码中通过清除栈顶并启动MainActivity实现应用重启,确保新Locale生效。
- 启动时加载保存的语言:重写
attachBaseContext方法,应用启动时优先使用用户上次选择的语言,而非系统默认语言。 - 适配高版本系统:Android 7.0及以上不能直接调用
updateConfiguration,必须通过createConfigurationContext创建新的配置上下文。
内容的提问来源于stack exchange,提问作者Rafael Bento
相关产品推荐
相关产品推荐

