how can this be prevented?
I am using a webview in my android app, however after it finishes loading - which I can detect via onPageFinished(WebView webview, String url) - the page continues to infinitely grow in height.
How can I prevent this from happening?
Here's what I do.
I have a generic page template which contains a ScrollView. Width and height are set to match parent. Inside the ScrollView there are several common elements for all the activities I create. One of them is a LinearLayout where I insert all the user content.
.....
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_margin="0dp"
android:padding="0dp"
android:scrollbars="none"
>
......
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:orientation="vertical">
//EVERY USER CONTENT GOES HERE
</LinearLayout>
......
</ScrollView>
.....
In this particular case the content is a web page, that loads Google maps using the mobile web API. Here is the layout file
<?xml version="1.0" encoding="utf-8"?>
<WebView
android:id="@+id/maps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="0dp"
android:padding="0dp"
/>
Here's the code of my activity that initialises the webview
.... WebView wv = (WebView)this.content.findViewById(R.id.maps);
WebSettings ws = wv.getSettings();
ws.setPluginState(PluginState.ON);
ws.setSupportZoom(false);
ws.setLightTouchEnabled(true);
ws.setDomStorageEnabled(true);
ws.setAppCacheMaxSize(1024 * 1024 * 8);
ws.setAppCachePath(context.getCacheDir().getAbsolutePath());
ws.setAppCacheEnabled(true);
ws.setAllowFileAccess(true);
ws.setCacheMode(WebSettings.LOAD_NORMAL);
ws.setJavaScriptEnabled(true);
wv.setInitialScale(0);
wv.addJavascriptInterface(new Object(), "Android");
wv.setKeepScreenOn(true);
Finally, when I load the maps via wv.loadUrl("......");
after the page finishes loading I get the following logs and the webview grows in an infinite loop
02-18 14:16:06.195: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.394: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.480: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.527: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.582: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.632: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.683: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.734: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.785: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.875: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:06.957: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.058: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.332: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.398: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.449: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.496: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.554: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.605: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.652: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.707: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.742: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.792: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.839: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.902: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:07.949: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.015: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.066: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.113: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.164: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.214: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.250: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.300: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.335: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.386: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.437: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.484: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.531: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.566: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.613: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.667: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.714: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.750: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.847: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.902: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:08.949: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.003: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.035: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.085: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.121: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.167: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.218: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.253: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.304: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.367: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.417: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.476: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.523: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.585: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.632: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.664: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.710: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.765: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.812: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.863: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.894: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.957: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.992: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.042: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.097: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.148: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.179: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.226: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.277: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.328: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.378: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.410: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.460: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.511: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.546: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.597: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.648: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.679: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.730: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.781: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.835: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.886: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.925: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:10.976: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.011: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.066: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.125: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.183: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.214: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.265: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.320: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.371: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.402: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.453: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.507: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.558: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.589: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:11.644: VERBOSE/webview(10904): OnSizeChanged: Enter
.......... This goes on ...........
I have noticed that this happens when the page contains jQuery. But the same page loads fine in the stock browser.
Any idea why this happens, and how can this be rectified?
Any meaningful help is appreciated... Thanks.
This is a late answer but hopefully it helps someone. I was experiencing the same problem and the culprit was this underlying CSS:
body {
width: 100%;
height: 100%;
}
This change fixes the issue:
body {
width: 100%;
height: auto;
}
This blog post solved my problem. I think it'll help. http://capdroid.wordpress.com/2014/08/07/resizing-webview-to-match-the-content-size/
private void setupWebView() {
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
super.onPageFinished(view, url);
}
});
webView.addJavascriptInterface(this, "MyApp");
}
@JavascriptInterface
public void resize(final float height) {
MyActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
}
});
}
Did you try android:layout_height="50dip"
Pass value for height attribute rather then using wrap_content
链接地址: http://www.djcxy.com/p/57662.html下一篇: 这怎么能被阻止?