Android WebView如何載入assets下的html檔案

來源:互聯網
上載者:User

標籤:android   color   ar   sp   for   檔案   on   問題   代碼   

項目需求:將html檔案以及所用到的圖片都放在 assets/html/ 目錄下。然後在頁面上通過WebView來顯示

 

直接付上代碼:

    private void readHtmlFormAssets(){
        WebSettings webSettings = tipsWebView.getSettings();
        
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        
        tipsWebView.setBackgroundColor(Color.TRANSPARENT);  //  WebView 背景透明效果,不知道為什麼在xml配置中無法設定?
        tipsWebView.loadUrl("file:///android_asset/html/tips.htm");
    }

 

以上就是常用的webView顯示設定,通過上段代碼就可以成功顯示html檔案,只要保證html代碼中圖片是相對位址,且能正確找到就能正常顯示網頁中所包含的圖片。(本例中圖片就位於同一目錄下。)

 

顯示效果:點擊時可以縮放,圖片正常顯示。初次顯示會縮放至合理的大小。因此上段代碼是以後的通用程式碼程式庫

註: WebView預設顯示效果就是100% px    顯示,我想要的效果是縮放到合理大小讓內容全部可見。所以你可以通過 setInitialScale來設定縮放,不過不同screen 需要設定不一樣的比例,而這個參數又不好在xml中設定,所以比較麻煩。用了上面的代碼,這個問題也給解決了。

 

I also experimented with setInitialScale but on different screen sizes and web page sizes that won‘t be as graceful as the browsers scaling.

Android WebView如何載入assets下的html檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.