我想在web視圖上把圖像加載到中間,但圖像顯示在web視圖的左上角。圖像是網絡的,而不是當地的。
gifView.loadUrl(mImageUrl);
gifView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setLoadsImagesAutomatically(true);
if (mProgressBar != null) {
mProgressBar.setVisibility(View.GONE);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (mProgressBar != null) {
mProgressBar.setVisibility(View.VISIBLE);
}
}
});
<WebView
android:id="@+id/gif_image"
android:background="@null"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
還添加了:
String HTML_FORMAT = "<html><body style=\"text-align: center; background-color: null; vertical-align: middle;\"><img src = \"%s\" /></body></html>";
final String html = String.format(HTML_FORMAT, mImageUrl);
gifView.loadDataWithBaseURL("", html, "text/html", "UTF-8", "");
我使用上面的代碼,怎麼把圖像加載到視圖的中間呢?
試試這行代碼:
myWebView.loadData("<html><head><style type='text/css'>body{margin:auto auto;text-align:center;} img{width:100%25;} </style></head><body><img src='www.mysite.com/myimg.jpeg'/></body></html>" ,"text/html", "UTF-8");