[Android学习系列2]用webview写界面,加载本地js,js,html文件 – 他来自一坨 – 推酷

以jquery mobile为例,webview需要加载assets文件夹中的文件需要以做file:///android_asset/绝对路径开头,加载的html文件其中引用的css、img、js等也需要使用file:///android_asset/绝对路径开头。

1.在android界面拖入一个webview,然后添加一个internet权限

 

<span class="tag">&lt;<span class="title">uses-<span style="color: #000000;">sdk</span></span>  </span>

android:minSdkVersion=“8”

android:targetSdkVersion=“18” />

<uses-permission android:name=“android.permission.INTERNET”/>

 

<application

………………………..

View Code

 

2.  在assets目录里面放入js,css,html资源文件

 

 

 

 

3.在写本地html的时候引入assert里的对应路径

 

 

<span style="color: #0000ff;"><span class="doctype">&lt;!</span></span><span class="doctype"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">&gt;</span></span>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />

<meta name=“viewport” content=“width=device-width, initial-scale=1”>

<title> 标题 </title>

<link rel=“stylesheet” type=“text/css” href=“file:///android_asset/css/jquery.mobile-1.4.2.min.css”>

<script src=“file:///android_asset/js/jquery-1.7.1.min.js”></script>

<script src=“file:///android_asset/js/jquery.mobile-1.4.2.min.js”></script>

</head>

<body>

 

 

<div data-role=“page”>

<div data-role=“header”>

<h1>My Title</h1>

</div>

 

<div data-role=“content”>

<ul data-role=“listview” data-inset=“true” >

<li><a href=“#”>Acura</a></li>

<li><a href=“#”>Audi</a></li>

<li><a href=“#”>BMW</a></li>

<li><a href=“#”>Cadillac</a></li>

<li><a href=“#”>Ferrari</a></li>

</ul>

</div>

</div><!– /page –>

 

</body>

</html>

View Code

 

4.在代码里访问页面

 

 

<span style="color: #0000ff;"><span class="keyword">package</span></span><span style="color: #000000;"> com.example.asd_webview;</span>

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.webkit.WebSettings;

import android.webkit.WebSettings.RenderPriority;

import android.webkit.WebView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

WebView webview = (WebView)findViewById(R.id.webView1);

WebSettings wv_setttig = webview.getSettings();

wv_setttig.setJavaScriptEnabled(true);

//wv_setttig.setRenderPriority(RenderPriority.HIGH);

 

String url = “file:///android_asset/index.html”;

webview.loadUrl(url);

 

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

View Code

 

5.最后效果如下:

 

 

来源URL:http://www.tuicool.com/articles/3mE7BzR