开发者

WebView redraw problem when loading data

开发者 https://www.devze.com 2023-03-25 23:12 出处:网络
I encounter a problem of display with a webview i\'m using in one of my app开发者_高级运维lication.

I encounter a problem of display with a webview i'm using in one of my app开发者_高级运维lication.

When I call loadData, the webview first display the text and then load the images of the page (standard behaviour).

Sometimes, if those images modify the text position, the old text position is not cleared and both old text and new text+image is displayed

Here a screenshot of what it looks like when the problem occurs:

WebView redraw problem when loading data

It's like the webview do not redraw correctly it's content. Of course a simple invalidate() does not work...

It do no occur often but lets say one time over 20.

Code used to display the data (called outside Activity life cycle methods):

// Include the html headers to the content received from WS
String webtext = String.format(ConfigApp.HTML_BODY, wsdata.getText());
mWebView.setWebChromeClient(...);
mWebView.setWebViewClient(...);
mWebView.setBackgroundColor(0);
mWebView.loadDataWithBaseURL(null, webtext, "text/html", "UTF-8", null);

Any idea how to fix this?


I've finally found a way to fix the problem.

Remove the WebView transparent background:

String webtext = String.format(ConfigApp.HTML_BODY, wsdata.getText());
mWebView.setWebChromeClient(...);
mWebView.setWebViewClient(...);
// mWebView.setBackgroundColor(0);
mWebView.loadDataWithBaseURL(null, webtext, "text/html", "UTF-8", null);

Because my WebView has a blank background it's ok for me. Maybe it can be annoying for somebody else.

For sure it's a WebView bug.


I have frequently had sporadic problems with WebViews not displaying properly after having loaded content with loadDataWithBaseUrl. Although the WebView's height was set to wrap_content, the WebView wouldn't always resize to wrap the content. Instead the webview had a height of zero. Sometimes it would display fine and other times it would not. Sometimes the problem would only happen in a portrait orientation and landscape would work fine. I tried removing my setBackgroundColor call but that it did not help. However, I was able to get the problem to go away today by changing the layout that the WebView was in from a LinearLayout to a RelativeLayout.


If you set your WebView to a specific size, and not "wrap_content" , then after the data is loaded, the webview doesn't resize its content correctly.

I think it's a bug with the widget..

So..
Try to use "wrap_content" in the layout_height attribute.


Try

mWebView.setBackgroundColor(0x00ff0000);


You just have to call refreshDrawableState() method after calling loadDataWithBaseURL() method and its will resolve your problem

mWebView.loadDataWithBaseURL(null, webtext, "text/html", "UTF-8", null);
mWebView.refreshDrawableState();
0

精彩评论

暂无评论...
验证码 换一张
取 消