1. 程式人生 > >WebView載入圖片居中問題

WebView載入圖片居中問題

      專案中涉及到WebView載入一張圖片,圖片的顯示不居中。在一個後臺小夥伴的幫助下搞定了。不得不感嘆術業有專攻啊。

      網上很多關於怎麼居中的,靠譜的沒幾個,無語

      直接貼程式碼了,備忘...

StringBuffer sb = new StringBuffer();
		 sb.append("<html>")
		.append("<head>")
		.append("<meta http-equiv='Content-Type' content='text/html'; charset='UTF-8'>")
		.append("<style type='text/css'>")
		.append(".response-img {max-width: 100%;}")
		.append("#box {width: 100%;height: 100%;display: table;text-align: center;background: #fff;}")
		.append("#box span {display: table-cell;vertical-align: middle;}")
		.append("</style>")
		.append("<title>")
		.append("</title>")
		.append("</head>")
		.append("<body style='text-align: center;' onClick='window.myInterfaceName.showToast(\"finish Activity\")'>")
		.append("<div id='box'>")
		.append("<span>")
		.append("<img src='" + imageUrl + "' class='response-img' style='width: 100%'/>")
		.append("</span>")
		.append("</div>")
		.append("</body>")
		.append("</html>");
		imgView.addJavascriptInterface(new JavascriptInterface(context),
				"myInterfaceName");
		imgView.loadDataWithBaseURL(null, sb.toString(),  "text/html", "UTF-8", null);