Android - WebView 语言在 Android 7.0 及更高版本上突然更改 [英] Android - WebView language changes abruptly on Android 7.0 and above

查看:32
本文介绍了Android - WebView 语言在 Android 7.0 及更高版本上突然更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多语言应用,主要语言为英语,次要语言为阿拉伯语.

<块引用>

然后导航到 WebView,区域设置从 ar-AEen-GB.

Android 7.0 API 更改:

Android 7.0 变化:

从 Android N 开始,Chrome 应用将用于渲染第三方 Android 应用中的任何/所有 WebView.在开箱即用的 Android N 手机中,Android WebView System 应用程序根本不存在.在已收到 Android N OTA 更新的设备中,Android 系统 WebView 被禁用:

此外,还引入了多区域支持,设备具有不止一种默认语言:

这对于具有多种语言的应用具有重要影响.如果您的应用程序具有 WebView s,那么它们将使用 Chrome 应用程序呈现.由于 Chrome 本身是一个 Android 应用程序,本身在自己的沙盒进程中运行,因此它不会绑定到您的应用程序设置的区域设置.相反,Chrome 将恢复到主要设备区域设置.例如,假设您的应用区域设置为 ar-AE,而设备的主要区域设置为 en-US.在这种情况下,包含 WebViewActivity 的区域设置将从 ar-AE 更改为 en-US, 并将显示来自相应语言环境文件夹的字符串和资源.您可能会在那些具有 WebViewActivity 上看到 LTR 和 RTL 字符串/资源的大杂烩.

解决方案:

这个问题的完整解决方案包括两个步骤:

第 1 步:

首先,在每个 Activity 或至少每个具有 WebViewActivity 中手动重置默认语言环境.

public static void setLocale(Locale locale){上下文上下文 = MyApplication.getInstance();资源资源 = context.getResources();配置配置 = resources.getConfiguration();Locale.setDefault(locale);configuration.setLocale(locale);如果(Build.VERSION.SDK_INT >= 25){context = context.getApplicationContext().createConfigurationContext(configuration);context = context.createConfigurationContext(configuration);}context.getResources().updateConfiguration(configuration,resources.getDisplayMetrics());}

在所有活动的 onCreate() 方法中调用 setContentView(...) 之前调用上述方法.locale 参数应该是您希望设置的默认 Locale.例如,如果您希望将阿拉伯语/UAE 设置为默认语言环境,则应传递new Locale("ar", "AE").或者,如果您希望设置默认区域设置(即操作系统自动设置的 Locale),您应该传递 Locale.US.

第 2 步:

另外,还需要添加如下代码行:

new WebView(this).destroy();

Application 类(如果有)的 onCreate() 中,以及用户可能更改语言的其他任何地方.这将处理更改语言后应用程序重新启动时可能发生的各种边缘情况(您可能已经注意到其他语言中的字符串或在更改 Activity 上的语言后具有相反对齐的字符串Activities代码>WebViews on Android 7.0++).

作为附录,Chrome 自定义标签现在是首选的渲染方式——应用网页.

参考:

1. Android 7.0 - WebView 的更改.

2. 了解 WebView 和 Android 安全补丁强>.

3. Android 版 WebView.

4. WebView:从由 Chrome 提供支持"到直接使用 Chrome.

5. 牛轧糖网络视图.

6. Android 7.0 牛轧糖.

7. Android N 之谜,第 1 部分:Android 系统 WebView 现在只是Chrome"?.

I have a multilingual app with primary language English and secondary language Arabic.

As described in the documentation,

  • I have added android:supportsRtl="true" in the manifest.
  • I have changed all xml properties with left and right attributes to start and end respectively.
  • I have added Arabic language strings in strings-ar (and similarly for other resources).

The above setup works properly. After changing the Locale to ar-AE, Arabic text & resources are correctly displayed in my Activities.

However, every time I navigate to an Activity with a WebView and/or a WebViewClient, the locale, text and layout direction abruptly revert to the device default.

Further hints:

  • This is occurring only on a Nexus 6P with Android 7.0. Everything works properly on Android 6.0.1 and below.
  • The abrupt shift in locale happens only when I navigate to an Activity that has a WebView and/or a WebViewClient (and I have several). It does not occur on any of the other Activities.

Android 7.0 has multi-locale support, allowing the user to set more than one default locale. So if I set the primary locale to Locale.UK:

Then on navigating to the WebView, the locale changes from ar-AE to en-GB.

Android 7.0 API changes:

As indicated in the list of API changes, new methods pertaining to locale have been added to the following classes in API 24:

Locale:

Configuration:

However, I am building my app with API 23, and am not using any of these new methods.

Furthermore ...

  • The problem occurs on the Nexus 6P emulator as well.

  • To get the default locale, I am using Locale.getDefault().

  • To set the default locale, I am using the following code:

    public static void setLocale(Locale locale){
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.setLocale(locale);
        Context context = MyApplication.getInstance();
        context.getResources().updateConfiguration(config,
                context.getResources().getDisplayMetrics());
    }
    

Has anyone encountered this problem before? What is the reason for it, and how do I resolve this?

References:

1. Native RTL support in Android 4.2.

2. Multilingual Support - Language and Locale.

3. Be wary of the default locale.

解决方案

Ted Hopp's answer managed to solve the problem, but he didn't address the question of why this occurs.

The reason is the changes made to the WebView class and its support package in Android 7.0.

Background:

Android's WebView is built using WebKit. While it was originally a part of AOSP, from KitKat onwards a decision was made to spin off WebView into a separate component called Android System WebView. It is essentially an Android system app that comes pre-installed with Android devices. It is periodically updated, just like other system apps such as Google Play Services and the Play Store app. You can see it in your list of installed system apps:

Android 7.0 changes:

Starting with Android N, the Chrome app will be used to render any/all WebViews in third-party Android apps. In phones that have Android N out-of-the-box, the Android WebView System app is not present at all. In devices that have received an OTA update to Android N, the Android System WebView is disabled:

and

Moreover, multi-locale support has been introduced, with devices having more than one default language:

This has an important consequence for apps that have multiple languages. If your app has WebViews, then those are rendered using the Chrome app. Because Chrome is an Android app in itself, running in its own sandboxed process, it will not be bound to the locale set by your app. Instead, Chrome will revert to the primary device locale. For example, say your app locale is set to ar-AE, while the primary locale of the device is en-US. In this case, the locale of the Activity containing a WebView will change from ar-AE to en-US, and strings and resources from the corresponding locale folders will be displayed. You may see a mish-mash of LTR and RTL strings/resources on those Activitys that have WebViews.

The Solution:

The complete solution to this problem consists of two steps:

STEP 1:

First, reset the default locale manually in every Activity, or at least every Activity that has a WebView.

public static void setLocale(Locale locale){
    Context context = MyApplication.getInstance();
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    Locale.setDefault(locale);
    configuration.setLocale(locale);

    if (Build.VERSION.SDK_INT >= 25) {
        context = context.getApplicationContext().createConfigurationContext(configuration);
        context = context.createConfigurationContext(configuration);
    }

    context.getResources().updateConfiguration(configuration,
            resources.getDisplayMetrics());
}

Call the above method before calling setContentView(...) in the onCreate() method of all your Activities. The locale parameter should be the default Locale that you wish to set. For example, if you wish to set Arabic/UAE as the default locale, you should pass new Locale("ar", "AE"). Or if you wish to set the default locale (i.e. the Locale that is automatically set by the operating system), you should pass Locale.US.

STEP 2:

Additionally, you need to add the following line of code:

new WebView(this).destroy();

in the onCreate() of your Application class (if you have one), and wherever else the user may be changing the language. This will take care of all kinds of edge cases that may occur on app restart after changing the language (you may have noticed strings in other languages or with the opposite alignment after changing the language on Activities that have WebViews on Android 7.0++).

As an addendum, Chrome custom tabs are now the preferred way of rendering in-app web pages.

References:

1. Android 7.0 - changes for WebView.

2. Understanding WebView and Android security patches.

3. WebView for Android.

4. WebView: From "Powered by Chrome" to straight up Chrome.

5. Nougat WebView.

6. Android 7.0 Nougat.

7. Android N Mysteries, Part 1: Android System WebView is just "Chrome" Now?.

这篇关于Android - WebView 语言在 Android 7.0 及更高版本上突然更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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