无法在奥利奥(Oreo)中更改语言 [英] Unable to change language in Oreo

查看:93
本文介绍了无法在奥利奥(Oreo)中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用中使用阿拉伯语和英语.在运行于Android Nougat或更低版本的设备上,其工作正常.但这不适用于oreo设备. API 26中是否有一些新的代码要求?我正在使用下面的代码.

I'm trying to use Arabic and English in my app. Its working fine on devices running on android Nougat or below. But it's not working on oreo devices. Is there some new code requirement in API 26? I am using the code below.

public void changeLanguage(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

并且我将"en"和"ar"作为语言参数传递.

and I'm passing "en" and "ar" as language argument.

推荐答案

设置新的Locale时,应重新启动Activity.您可以使用下一个代码片段执行该操作:

When you set new Locale you should restart your Activity. You can perform it using the next snippet of code:

private void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

然后您的changeLanguage()方法将以另一种方式显示:

Then your changeLanguage() method will look in a next way:

public void changeLanguage(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

    restartActivity();
}

这篇关于无法在奥利奥(Oreo)中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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