Android KitKat 中的 WebView 渲染问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20675554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:16:08  来源:igfitidea点击:

WebView Rendering Issue in Android KitKat

androidwebviewchromiumandroid-4.4-kitkat

提问by gnuanu

I've been working on an application which have a WebView in which a static page get loaded from the assets (Also using JavaScript). This WebView is not working in KitKat, it remains blank. I am aware of the change in rendering engine (webkit to chromium) which happened in WebView in kitkat and tried the steps for migrating, which is given in Android Developers page. But it didn't help.

我一直在开发一个具有 WebView 的应用程序,其中从资产加载静态页面(也使用 JavaScript)。此 WebView 在 KitKat 中不起作用,它保持空白。我知道在 kitkat 的 WebView 中发生的渲染引擎(webkit 到chromium)的变化,并尝试了迁移步骤,这在Android 开发人员页面中给出。但它没有帮助。

In logcat I am getting an error which is thrown from the Chromium source.

在 logcat 中,我收到从 Chromium 源抛出的错误。

W/AwContents﹕ nativeOnDraw failed; clearing to background color.

W/AwContents﹕ nativeOnDraw failed; clearing to background color.

Please suggest a workaround.

请提出解决方法。

回答by georgiecasey

In my case, in Android 4.4, I was getting a black background no matter I set what and this error message in my LogCat: nativeOnDraw failed; clearing to background color.

就我而言,在 Android 4.4 中,无论我设置什么,我都会得到黑色背景,并且在我的 LogCat 中出现此错误消息:nativeOnDraw failed; 清除为背景颜色。

From Googling, it seems to be because hardware accelerated canvas rendering is not supported in Chromium WebView. I added this line to the WebView to turn off hardware accelerated canvas and now it works.

从谷歌搜索,似乎是因为 Chromium WebView 不支持硬件加速画布渲染。我将此行添加到 WebView 以关闭硬件加速画布,现在它可以工作了。

mWebview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

回答by DataDino

I ran into the same issue, but I did find a workaround. All you have to do is explicitly set a CSS background for your webpage. Like so:

我遇到了同样的问题,但我确实找到了解决方法。您所要做的就是为您的网页明确设置 CSS 背景。像这样:

body {
  background: white;
}

As it turns out if you do not explicitly set a background for a webpage the WebView will fail to draw said background and you'll end up with a transparent WebView.

事实证明,如果您没有为网页明确设置背景,WebView 将无法绘制所述背景,最终您将获得一个透明的 WebView。

回答by Tore Rudberg

This seems to be a chromium webview bug.

这似乎是一个 Chromium webview 错误。

Here is a thread about the issue: https://jira.appcelerator.org/browse/TIMOB-16479

这是关于这个问题的一个线程:https: //jira.appcelerator.org/browse/TIMOB-16479

Apparently, the accepted answer is not a sure fix. A workaround is mentioned in the link.

显然,接受的答案不是一个确定的解决方案。链接中提到了一种解决方法。

回答by asiop

The disabling of the hardware accelerator comes with heavy performance toll, In my case I found out that in Kitkat this happened to me when I was re instantiating the webview element within an activity that was finished and later restarted. After a lot of trial and error, when I added:

禁用硬件加速器会带来沉重的性能损失,就我而言,我发现在 Kitkat 中,当我在已完成并随后重新启动的活动中重新实例化 webview 元素时,这发生在我身上。经过大量的反复试验,当我添加:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.webViewContainer);
layout.removeAllViews();
webview.destroy();

Just before ending the activity, it seems to have the problem solved. I haven't tested it on many devices yet but if this solution is proper, then it is better by far than disabling the hardware acceleration for KitKat for the webview.

就在活动结束前,问题似乎已经解决了。我还没有在很多设备上测试过它,但如果这个解决方案是正确的,那么它比禁用 webview 的 KitKat 的硬件加速要好得多。

回答by Raj008

package com.example.testandroid;



public class MainActivity extends ActionBarActivity {

WebView webView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (savedInstanceState != null)
    {
        ((WebView)findViewById(R.id.web_view)).restoreState(savedInstanceState);
    }
    else{

        webView=(WebView)findViewById(R.id.web_view);
        webView.loadUrl("http://www.google.co.in");
        webView.getSettings().getJavaScriptEnabled();
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        webView.setWebViewClient(new WebViewClient()

        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,
                    String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
    }
}


protected void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}