您可以通过以下方式构建本地WebView
- 活动 (LocalWebviewActivity.java)
- 布局 (activity_localwebview.xml)
- Assets 文件夹(在“assets”文件夹的根目录下,创建文件夹“css”并将“style.css”放在这里)
- 引用 JS 文件的方式与引用 CSS 样式表的方式相同
LocalWebviewActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class LocalWebviewActivity extends Activity {
    WebView myWebView;
    StringBuilder mySBcontent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_localwebview);
        myWebView = (WebView) findViewById(R.id.webkit);
        mySBcontent = new StringBuilder();
        mySBcontent.append("<html>");
        mySBcontent.append("<head>");
        mySBcontent.append("<link type='text/css' rel='stylesheet' href='css/style.css'>");
        mySBcontent.append("</head>");
        mySBcontent.append("<body>");
        mySBcontent.append("<h1>My Heading</h1>");
        mySBcontent.append("<p>My HTML content</p>");
        mySBcontent.append("<p><img style='width:150px;' src='myImg.png' /></p>");
        mySBcontent.append("</body>");
        mySBcontent.append("</html>");
        myWebView.loadDataWithBaseURL("file:///android_asset/", mySBcontent.toString(), "text/html", "UTF-8", "");
    }
}
activity_localwebview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <WebView 
        android:id="@+id/webkit"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>