Android WebViewローカルhtmlおよびcssファイルの読み込み

1777 ワード

MainActivity.java
package com.example.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		WebView wvTest = (WebView) findViewById(R.id.webView1);
		wvTest.loadUrl("file:///android_asset/test.html"); //   asset     test.html  		
	}
}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>     </title>
<link rel="stylesheet" type="text/css" href="file:///android_asset/test.css"/>
</head>

<body>

<div class="test">zzzzzzzzzzzzz</div>

<img src="file:///android_asset/test.png" />
</body>
</html>