我的 android 项目正在 webview 中加载一个 html 文件,其中包含一个带有切片图像的表格。加载 webview 时,图像对齐到手机屏幕的左上角。图像可以缩放到手机屏幕的宽度。如何在屏幕内垂直居中图像?
代码:
WebView browser = (WebView) actividad.findViewById(R.id.webView_map);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
browser.getSettings().setBuiltInZoomControls(true);
browser.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
browser.setPadding(0, 0, 0, 0);
browser.setInitialScale(getScale());
browser.loadUrl(imageHtml);
private final static int MAP_WIDTH = 2500;
private int getScale(){
Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).
getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
Double val = Double.valueOf(dm.widthPixels)/Double.valueOf(MAP_WIDTH);
val = val * 100d;
return val.intValue();
}
HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="mapa.css" />
</head>
<body>
<div class="wrapper">
<table cellpadding="0" border="0" cellspacing="0">
<tr>
<td><img alt=" " src="mapa_general/mapa_general_0_0.png" style=" border-width: 0px;"></td>
<td><img alt=" " src="mapa_general/mapa_general_0_1.png" style=" border-width: 0px;"></td>
</tr>
<tr>
<td><img alt=" " src="mapa_general/mapa_general_1_0.png" style=" border-width: 0px;"></td>
<td><img alt=" " src="mapa_general/mapa_general_1_1.png" style=" border-width: 0px;"></td>
</tr>
</table>
</div>
</body>
</html>
CSS:
body, html {
height: 100%;
width: 100%;
}
body {
display: table;
}
#wrapper {
display: table-cell;
vertical-align: middle;
}