天天看點

android studio中建立assets,用webview加載本地圖檔

xml檔案中建立webview

<WebView
        android:id="@+id/wv_explain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
    <span style="white-space:pre">	</span>/>
           

在project下,main檔案夾下建立assets檔案,建web,最後images,在images裡放需要加載的圖檔,在web中加入以該圖檔命名的html,其代碼為:

<pre name="code" class="html"><html>
<head>
<title>explain</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- Save for Web Slices -->
<img src="images/explain.png" width="1080" height="3400" alt="">
<!-- End Save for Web Slices -->
</body>
</html>
           

最後在主代碼裡,申明webview,加入以下代碼:

WebSettings webSettings = wvExplain.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        wvExplain.getSettings().setSupportZoom(true);
        wvExplain.setBackgroundColor(Color.TRANSPARENT);
        wvExplain.loadUrl("file:///android_asset/web/explain.html");
           

至此,好像是可以了吧?