如何更改Android O / Oreo / api 26应用程序语言 [英] How to change Android O / Oreo / api 26 app language

查看:130
本文介绍了如何更改Android O / Oreo / api 26应用程序语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改应用程序的语言,直到API 26才能正常工作。

I want to change the language of the app and this works fine until API 26.

对于api> 25,我放了 Locale.setDefault (Locale.Category.DISPLAY,mynewlanglocale); setContentView(R.layout.activity_main); 之前,但没有任何变化。

For api > 25 I put Locale.setDefault(Locale.Category.DISPLAY, mynewlanglocale); before setContentView(R.layout.activity_main); but nothing changes.

docs 对此没有太多解释。

推荐答案

是在android中Oreo本地化在updateconfiguration中运行不正常。但它在Android N本身被弃用了。而不是更新配置使用每个attachcontext中的createconfiguration。它对我来说很好。试试这个......

Yes in android Oreo localization is not working fine with updateconfiguration. But it is deprecated in android N itself. Instead of updateconfiguration use createconfiguration in each attachcontext. it is working fine for me. Try this...

在你的活动中添加这个..

In you activity add this..

@Override
protected void attachBaseContext(Context newBase) {
    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        super.attachBaseContext(MyContextWrapper.wrap(newBase, "ta"));
    }
    else {
        super.attachBaseContext(newBase);
    }
}

在MyContextWrapper.java中

In MyContextWrapper.java

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);
        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);

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

    return new ContextWrapper(context);
}

这篇关于如何更改Android O / Oreo / api 26应用程序语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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