【发布时间】:2014-09-26 09:22:33
【问题描述】:
有一个 webview 我禁用了滚动。并且它等于android手机屏幕尺寸的宽度。
问题是 webview 中的内容不是自动调整大小而是显示在 webview 之外(因为我禁用了滚动,但 webview 大小不是“完全”屏幕宽度),你可以看看 screenshot
我已经添加了
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'>
和
newsContent.getSettings().setUseWideViewPort(true);
newsContent.getSettings().setLoadWithOverviewMode(true);
但还是不行。谢谢。
Web 视图 XML:
<WebView
android:id="@+id/newsContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:background="#ffffff" />
Webview JAVA:
StringBuilder sb = new StringBuilder();
sb.append("<HTML><HEAD><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0'><LINK href=\"news.css\" type=\"text/css\" rel=\"stylesheet\"/><script src=\"jquery-1.10.2.js\" type=\"text/javascript\"></script></HEAD><body>");
sb.append(newsItem.description.toString());
sb.append("<script>$('img').on('click', function() {app.zoom($(this).attr('src'));});</script></body></HTML>");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
newsContent.getSettings().setAllowUniversalAccessFromFileURLs(true);
newsContent.getSettings().setAllowFileAccessFromFileURLs(true);
}
newsContent.setWebChromeClient(new WebChromeClient());
newsContent.setWebViewClient(new WebViewClient());
newsContent.getSettings().setUseWideViewPort(true);
newsContent.getSettings().setLoadWithOverviewMode(true);
newsContent.getSettings().setJavaScriptEnabled(true);
newsContent.addJavascriptInterface(new WebViewJavaScriptInterface(), "app");
newsContent.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html", "utf-8", null);
newsContent.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
newsContent.setVerticalScrollBarEnabled(false);
newsContent.setHorizontalScrollBarEnabled(false);
【问题讨论】:
-
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null); -
视图太大,无法放入绘图缓存,需要 7890400 字节,只有 3686400 可用
-
webview.setDrawingCacheEnabled(false); -
如果this适合你,你可以试试。
-
谢谢。它适用于图像,但文本不会自动适应并创建下一行
标签: android html css android-layout webview