嗨,我有一个 webview,我希望它适合屏幕,这样用户就不必水平或垂直滚动
我之前搜索过,很多人建议以下但它只限制宽度,用户仍然需要垂直滚动:
engine = (WebView) (findViewById(R.id.webView1));
engine.loadUrl(www.example.com);
engine.getSettings().setLoadWithOverviewMode(true);
engine.getSettings().setUseWideViewPort(true);
我尝试了另一种方法,使用缩放方法,但它不起作用.....我得到了窗口宽度(1024 像素),网页内容宽度也是 1024 像素!!所以缩放不起作用....有没有什么方法可以得到正确的比例?
或者....还有其他方法吗?....谢谢!!
// get the window width and height
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
width = outSize.x;
height = outSize.y;
Log.i("Menuboard", "width: " + Integer.toString(width) + ", height: "
+ Integer.toString(height));
WebViewClient client = new WebViewClient() {
public void onPageFinished(WebView view, String url) {
int x = engine.getWidth();
int y = engine.getHeight();
Log.i("Menuboard", "web width: " + Integer.toString(x)
+ ", height: " + Integer.toString(y));
int scale1 = width * 100/x;
int scale2 = height * 100/y;
Log.i("Menuboard", "Scale1: " + Integer.toString(scale1)
+ ", Scale2: " + Integer.toString(scale2));
if (scale1 < scale2) {
engine.setInitialScale(scale1);
Log.i("Menuboard", "scale1");
} else if (scale1 > scale2) {
engine.setInitialScale(scale2);
Log.i("Menuboard", "scale2: " + Integer.toString(scale2));
}
}
};
engine = (WebView) (findViewById(R.id.webView1));
engine.loadUrl(string);
engine.getSettings().setBuiltInZoomControls(true);
engine.setPadding(0, 0, 0, 0);
engine.setWebViewClient(client);
只是一些注释:
要缩放 web 视图:
engine = (WebView) (findViewById(R.id.webView1));
engine.loadUrl(www.example.com);
engine.getSettings().setBuiltInZoomControls(true);
engine.setPadding(0, 0, 0, 0);
engine.setInitialScale(50); // 0 - 100 where 100 is full scale
获取窗口宽度和高度:
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
width = outSize.x;
height = outSize.y;