webview與HTML加載自適應問題解決方案(網頁拖動右邊有空白)
前幾天項目遇到了webview加載HTML的時候,網頁拖動右邊有空白的現象,找了很多方案,都沒解決,我研究了一下,其實很簡單,特把方法列出來,供大家參考一下,希望能對大家有所幫助。先看效果圖
怎麼拖都不會動,同時加 有對網頁放大和縮小的功能。
先把代碼拿出來,HTML的我是PHP寫的,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><? echo $arr['subject'];?></title>
<style type="text/css">
<!--
*{ margin:0; padding:0}
body{
margin-left:0;
margin-right:0;
}
.STYLE1 {color: #006699; font-weight:bold}
-->
</style>
</head>
<body>
<table width="98%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td ><span ><? echo $arr['subject'];?></span></td>
</tr>
<tr>
<td >作者:<? echo $arr[author];?> 時間:<? echo tranTime($arr[postdate]);?></td>
</tr>
<tr>
<td><? echo $content;?></td>
</tr>
</table>
</body>
</html>
跟一般的寫法沒什麼兩樣,其中上麵加載PHP的我省略掉了,相信大家能看明白。
下麵是JAVA代碼
String url="https://www.dengwei1999.com/newslist.php?id=8";
wv = (WebView) findViewById(R.id.webView1);
wv.setVisibility(WebView.VISIBLE);
WebSettings ws = wv.getSettings();
//ws.setUseWideViewPort(true);
ws.setJavaScriptEnabled(true);
wv.addJavascriptInterface(new ContactsPlugin(), "contactsAction");
//設置可以支持縮放
wv.getSettings().setSupportZoom(true);
//設置默認縮放方式尺寸是far
wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
//設置出現縮放工具
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl(url);
就這樣就可以實現了,是不是很簡單。嗬嗬
最後更新:2017-04-02 17:28:40