如何解决在 AndroidX 中使用区域设置和夜间模式更改发生冲突
我尝试使用夜间模式并更改应用程序中的语言环境(使用 appcompat:1.1.0 和 appcompat:1.2.0 版本),但是当活动再次开始时,语言环境没有实现任何更改。这是我的代码
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(updateBaseContextLocale(base));
}
@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context,Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration);
}
private Context updateBaseContextLocale(Context context) {
String language = LocaleHelper.getLanguage(context); // Helper method to get saved language from SharedPreferences
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResourcesLocale(context,locale);
}
return updateResourcesLocaleLegacy(context,locale);
}
@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context,Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration,resources.getdisplayMetrics());
return context;
}
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
我像这样更改主题AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。