Android N 以编程方式更改语言 [英] Android N change language programmatically

查看:34
本文介绍了Android N 以编程方式更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了仅在 Android N 设备上重现的非常奇怪的错误.

I found really weird bug that is reproduced only on Android N devices.

在浏览我的应用程序时,可以更改语言.这是更改它的代码.

In tour of my app there is a possibility to change language. Here is the code that changes it.

 public void update(Locale locale) {

    Locale.setDefault(locale);

    Configuration configuration = res.getConfiguration();

    if (BuildUtils.isAtLeast24Api()) {
        LocaleList localeList = new LocaleList(locale);

        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        configuration.setLocale(locale);

    } else if (BuildUtils.isAtLeast17Api()){
        configuration.setLocale(locale);

    } else {
        configuration.locale = locale;
    }

    res.updateConfiguration(configuration, res.getDisplayMetrics());
}

此代码在我的巡回演出活动中效果很好(使用 recreate() 调用),但在所有接下来的活动中,所有 String 资源都是错误的.屏幕旋转修复了它.遇到这个问题我该怎么办?我应该以不同的方式更改 Android N 的语言环境还是只是系统错误?

This code works great in activity of my tour ( with recreate() call) but in all next activities all String resources are wrong. Screen rotation fixes it. What can i do with this problem? Should i change locale for Android N differently or it's just system bug?

附言这是我发现的.MainActivity 的第一次启动(这是我的巡回演出之后)Locale.getDefault() 是正确的,但资源是错误的.但是在其他活动中,它给了我错误的语言环境和来自该语言环境的错误资源.旋转屏幕后(或者其他一些配置更改)Locale.getDefault() 是正确的.

P.S. Here's what i found. At first start of MainActivity (which is after my tour) Locale.getDefault() is correct but resources are wrong. But in other activities it gives me wrong Locale and wrong resources from this locale. After rotation screen (or perhaps some other configuration change) Locale.getDefault() is correct.

推荐答案

好的.最后我设法找到了解决方案.

Ok. Finally i managed to find a solution.

首先您应该知道在 25 API 中 Resources.updateConfiguration(...) 已被弃用.因此,您可以执行以下操作:

First you should know that in 25 API Resources.updateConfiguration(...) is deprecated. So instead you can do something like this:

1) 您需要创建自己的 ContextWrapper,它将覆盖 baseContext 中的所有配置参数.例如,这是我的 ContextWrapper,它正确更改了语言环境.注意 context.createConfigurationContext(configuration) 方法.

1) You need to create your own ContextWrapper that will override all configuration params in baseContext. For example this is mine ContextWrapper that changes Locale correctly. Pay attention on context.createConfigurationContext(configuration) method.

public class ContextWrapper extends android.content.ContextWrapper {

    public ContextWrapper(Context base) {
        super(base);
    }

    public static ContextWrapper wrap(Context context, Locale newLocale) {
        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

        if (BuildUtils.isAtLeast24Api()) {
            configuration.setLocale(newLocale);

            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);

            context = context.createConfigurationContext(configuration);

        } else if (BuildUtils.isAtLeast17Api()) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);

        } else {
            configuration.locale = newLocale;
            res.updateConfiguration(configuration, res.getDisplayMetrics());
        }

        return new ContextWrapper(context);
    }
}

2) 以下是您应该在 BaseActivity 中执行的操作:

2) Here's what you should do in your BaseActivity:

@Override
protected void attachBaseContext(Context newBase) {

    Locale newLocale;
    // .. create or get your new Locale object here.

    Context context = ContextWrapper.wrap(newBase, newLocale);
    super.attachBaseContext(context);
}

注意:

如果您想更改区域设置,请记住重新创建您的活动你的应用程序在某处.您可以覆盖您想要使用的任何配置这个解决方案.

Remember to recreate your activity if you want to change Locale in your App somewhere. You can override any configuration you want using this solution.

这篇关于Android N 以编程方式更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆