这怎么能被阻止?
我在我的android应用程序中使用webview,但是在加载完成后 - 我可以通过onPageFinished(WebView webview,String url)检测到 - 页面高度继续无限增长。
我怎样才能防止这种情况发生?
这就是我所做的。
我有一个通用页面模板,其中包含一个ScrollView。 宽度和高度设置为与父级相匹配。 在ScrollView中,我创建的所有活动都有几个通用元素。 其中之一是我插入所有用户内容的LinearLayout。
.....
<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>
.....
在这种特殊情况下,内容是一个网页,它使用移动网页API加载谷歌地图。 这是布局文件
<?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"
/>
以下是我的活动代码,用于初始化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);
最后,当我通过wv.loadUrl(“......”)加载地图时,
在页面加载完成后,我得到以下日志,并且webview以无限循环增长
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 ...........
我注意到,当页面包含jQuery时会发生这种情况。 但同一页面在股票浏览器中正常加载。
任何想法为什么发生这种情况,以及如何纠正?
任何有意义的帮助表示赞赏...谢谢。
这是一个迟到的答案,但希望它可以帮助某人。 我遇到了同样的问题,罪魁祸首就是这个底层CSS:
body {
width: 100%;
height: 100%;
}
此更改修复了以下问题:
body {
width: 100%;
height: auto;
}
这篇博文解决了我的问题。 我认为这会有所帮助。 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)));
}
});
}
你尝试过android:layout_height =“50dip”
传递height属性的值,而不是使用wrap_content
链接地址: http://www.djcxy.com/p/57661.html上一篇: how can this be prevented?
下一篇: Log the Bandwidth usage and request time in ASP.NET 4.0