gpt4 book ai didi

android - 使用类 ContextWrapper 在 Release模式下不工作更改语言 Android 应用程序

转载 作者:行者123 更新时间:2023-11-30 05:02:23 26 4
gpt4 key购买 nike

我已实现此代码以更改应用程序的语言,它在具有 API 23、24、25、26、27 和 28 的模拟器中以及安装在 “Active Build”中时在真实设备中也能正常工作“调试”状态下的“变体”模式

信息: Android N change language programmatically

但是当我在 “release” 状态下以 “Active Build Variant” 模式将应用程序上传到 Google Play 商店时,语言更改在真实设备上不起作用

这可能会发生吗?我不知道该怎么办这个问题了。如果你能帮助我,我将不胜感激。谢谢

我的方法静态 ContextWrapper,类 Config:

    public static ContextWrapper changeLang(Context context, String lang_code){
Locale sysLocale;

Resources rs = context.getResources();
Configuration config = rs.getConfiguration();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = config.getLocales().get(0);
} else {
sysLocale = config.locale;
}

if ( !lang_code.equals("") ) {
Locale locale = new Locale(lang_code);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}

return new ContextWrapper(context);
}

我的 Activity 代码

    @Override
protected void attachBaseContext(Context newBase) {
pref = PreferenceManager.getDefaultSharedPreferences( newBase );
String lang_code = pref.getString("LANG_APP","");

Context context = Config.changeLang(newBase, lang_code);
super.attachBaseContext(context);
}

这可能会发生吗?我不知道该怎么办这个问题了。如果你能帮助我,我将不胜感激。谢谢

最佳答案

This解决方案对我有用

android {
bundle {
language {
// Specifies that the app bundle should not support
// configuration APKs for language resources. These
// resources are instead packaged with each base and
// dynamic feature APK.
enableSplit = false
}
}
}

问题可能是 Android BUNDLE,如果您构建 BUNDLE(不是 apk),它会删除 apk 文件中的语言 .xml。如果您编写以上代码,则在创建 .aab bundle 文件时不会删除 language.xml。

关于android - 使用类 ContextWrapper 在 Release模式下不工作更改语言 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58035524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com