当前位置:   article > 正文

升级AndroidX appcompat 1.2+版本导致多语言切换失败解决方案及问题分析_shouldsupportmultilanguage()

shouldsupportmultilanguage()

appcompat升级1.2版本之前,可以通过覆写ApplicationAppCompatActivityattachBaseContext方法,修改ContextResources下的Configration的local内容来达到语言切换。

@Override
protected void attachBaseContext(Context newBase) {
   
    if (shouldSupportMultiLanguage()) {
   
        Integer language = SpUtil.getInt(newBase, Cons.SP_KEY_OF_CHOOSED_LANGUAGE, -1);
        super.attachBaseContext( LanguageUtil.attachBaseContext(newBase, language));
    } else {
   
        super.attachBaseContext(newBase);
    }
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

但是appcompat升级1.2版本之后,这样写发现无效了,出现问题的原因就在于AppCompatActivity中获取的Resources 并非我们设置切换目标语言的Resources,而是 appcompat框架额外包裹一层ContextThemeWrapper中的 Resources。下面先给出解决方案,之后分析为什么升级后会出现这样的问题。

// BaseActivity继承AppCompatActivity
// 修复appcompat 1.2+版本导致多语言切换失败,传自定义的ContextThemeWrapper
@Override
protected void attachBaseContext(Context newBase) {
   
    if (shouldSupportMultiLanguage()) {
   
        Integer language = SpUtil.getInt(newBase, Cons.SP_KEY_OF_CHOOSED_LANGUAGE, -1);
        Context context = LanguageUtil.attachBaseContext(newBase, language);
        final Configuration configuration = context.getResources().getConfiguration();
        // 此处的ContextThemeWrapper是androidx.appcompat.view包下的
        // 你也可以使用android.view.ContextThemeWrapper,但是使用该对象最低只兼容到API 17
        // 所以使用 androidx.appcompat.view.ContextThemeWrapper省心
        final ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context,
            R.style.Theme_AppCompat_Empty) {
   
            @Override
            public void applyOverrideConfiguration(Configuration overrideConfiguration) {
   
                if (overrideConfiguration != null) {
   
                    overrideConfiguration.setTo(configuration);
                }
                super.applyOverrideConfiguration(overrideConfiguration);
            }
        };
        super.attachBaseContext(wrappedContext);
    } else {
   
        super.attachBaseContext(newBase);
    }
}

// 下面是多语言切换方法
public static Context attachBaseContext(Context context, Integer language) {
   
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   
        return createConfigurationResources(context, language);
    } else {
   
        applyLanguage(context, language);
        return context;
    }
}

public static void applyLanguage(Context context, Integer newLanguage) {
   
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    Locale locale = getSupportLanguage(newLanguage
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号