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

查看:161
本文介绍了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() call)但在所有下一个活动中,所有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?

P.S。这是我发现的。首次启动MainActivity(在我的巡演之后) Locale.getDefault()是正确的,但资源是错误的。但在其他活动中,它给了我错误的Locale和来自此语言环境的错误资源。旋转屏幕(或可能是其他一些配置更改) 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,可以正确地更改Locale。注意 context.createConfigurationContext(配置)方法。

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天全站免登陆