安卓 WebView載入本地圖片時置中顯示

來源:互聯網
上載者:User

標籤:android   使用   width   問題   html   c   

在一個項目中使用WebView顯示gif圖片(自訂的View無法放大gif),當圖片過小時只在左側顯示,經過研究發現無論設定android:layout_gravity="center_horizontal"還是設定android:gravity="center_horizontal" 都無法置中顯示,而且還設定了android:layout_width="wrap_content",但是實際上WebView並沒有自適應內容,它的寬度佔了螢幕寬度。

當時使用WebView載入本地圖路時使用的是this.webViewGif.loadUrl(gifFilePath);方式,其實WebView是為了顯示Html內容,因此可以將圖片資源放在一個HTML內容中:

String gifFilePath = "file://" + this.filePath;

String data = "<HTML><Div align=\"center\"  margin=\"0px\"><IMG src=\""+gifFilePath+"\" margin=\"0px\"/></Div>"; 

webViewGif.loadDataWithBaseURL(gifFilePath, data, "text/html", "utf-8", null);

問題解決。

 

另:我只是為了顯示gif圖片,並且不讓大的gif圖片過寬時左右滑動,設定了webViewGif.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.