更改语言 Android 应用程序与类 ContextWrapper 无法在发布模式下工作

Lui*_*uis 6 android android-studio

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

信息: Android N 以编程方式更改语言

但是,当我在“发布”状态下以“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);
}
Run Code Online (Sandbox Code Playgroud)

我的活动代码

    @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);
}
Run Code Online (Sandbox Code Playgroud)

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

小智 0

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
    super.applyOverrideConfiguration(baseContext.resources.configuration)
}
Run Code Online (Sandbox Code Playgroud)

我遇到了同样的问题,将其添加到基本活动中为我解决了这个问题


归档时间:

查看次数:

1297 次

最近记录:

3 年,11 月 前