Androidx多语言切换

项目升级Androidx后多语言失效

项目原本使用support版本,多语言切换时可以实现,但是升级到Androidx发现切换语言并没有生效,所以也找了几篇文章看了,发现都是说引用了androidx.appcompat:appcompat:1.2.+包引起的,需要重写attachBaseContext(Context context)方法,于是我再application中加入了该方法的重写,运行完发现,还是不行。
最后解决:在BaseActivity类中重写attachBaseContext(Context context)方法
具体引起失效原因请看这篇文章:https://blog.csdn.net/jdsjlzx/article/details/109285503

实现工具类:
/**
 * 语言切换
 *
 * @author chends create on 2019/3/20.
 */
public class LanguageUtil {
    private static LanguageUtil mLanguageUtil;

    public static final String LANGUAGE_SYSTEM = "auto";//跟随系统
    public static final String LANGUAGE_ENGLISH = "en";
    public static final String LANGUAGE_CHINESE = "zh";
    private static final String LANGUAGE_KEY = "language_key";
    private static SharedPreferences prefs;
    public synchronized static LanguageUtil getInstance() {
        if(mLanguageUtil == null){
            synchronized (LanguageUtil.class){
                if(mLanguageUtil == null){
                    mLanguageUtil = new LanguageUtil();
                    prefs = PreferenceManager.getDefaultSharedPreferences(EmailApplication.getContext());
                }
            }
        }
        return mLanguageUtil;
    }

    public Context setLocale(Context c) {
        return updateResources(c, getLanguage());
    }

    public Context setNewLocale(Context c, String language) {
        persistLanguage(language);
        return updateResources(c, language);
    }

    public String getLanguage() {
        return prefs.getString(LANGUAGE_KEY, LANGUAGE_SYSTEM);
    }

    private void persistLanguage(String language) {
        prefs.edit().putString(LANGUAGE_KEY, language).commit();
    }

    private Context updateResources(Context context, String language) {
        Locale locale;
        Resources res = context.getResources();
        if(language.equals("auto")){
            locale = getLocale(res);
        }else{
            locale = new Locale(language);
        }
        Locale.setDefault(locale);

        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= JELLY_BEAN_MR1) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

    public static Locale getLocale(Resources res) {
        Configuration config = res.getConfiguration();
        return Build.VERSION.SDK_INT >= N ? config.getLocales().get(0) : config.locale;
    }
}
切换语言
//获取当前语言
LanguageUtil.getInstance().getLanguage()//切换语言
LanguageUtil.getInstance().setNewLocale(this,LanguageUtil.LANGUAGE_CHINESE);
//重启Main
Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
在BaseActivity中重写attachBaseContext方法

通过attachBaseContext传递的对象需要是androidx.appcompat.view.ContextThemeWrapper解决方案就是让AppCompatActivity中attachBaseContext方法代理程序进入此段代码来达到返回自定义的ContextThemeWrapper

 @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtil.getInstance().setLocale(newBase));
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ぃ随风逍遥う

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值