【发布时间】:2020-03-13 03:05:46
【问题描述】:
我在手动更改应用程序语言时遇到问题,在应用程序中,我让用户能够将应用程序的语言更改为他们喜欢的语言,下面的代码即使在 Android 中也能正常工作(Pixel 3 Emulator ),但由于某种原因,它不适用于所有三星设备
Context context = LocaleUtils.setLocale(getApplicationContext(), languageCode);
Resources resources = context.getResources();
Locale myLocale = new Locale(languageCode);
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration conf = resources.getConfiguration();
conf.locale = myLocale;
resources.updateConfiguration(conf, dm);
Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(
getBaseContext().getPackageName());
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
应用类:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
LocaleUtils.onAttach(base, Locale.getDefault().getLanguage());
MultiDex.install(this);
}
在每个活动上:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(ViewPumpContextWrapper.wrap(LocaleUtils.onAttach(newBase)));
}
【问题讨论】:
-
我也有同样的问题。我尝试了多个库但没有成功。我终于设法拿到了三星设备,并调试了在某些情况下在活动中调用 getResources() 会返回将其配置设置回原始语言环境的资源。似乎在某些情况下,其他东西会覆盖活动中的语言环境。 PS:我在每个活动的 onCreate 方法中覆盖了语言环境。
-
我刚刚设法为我拥有的设备修复了它。我最终使用了这个解决方案stackoverflow.com/a/59370534/3296947,并在调用 super() 之后立即在活动的 attachBaseContext 中调用它。有关此主题的更多信息:stackoverflow.com/questions/55265834/…
标签: java android locale android-10.0