51CTO曾经独家推荐过Android开发应用详解的专题,本文希望通过本次对WebView组件的使用讲解,可以让各位了解到WebView组件的详细使用:
网络内容
1、LoadUrl直接显示网页内容(单独显示网络图片)
2、LoadData显示中文网页内容(含空格的处理)
APK包内文件
1、LoadUrl显示APK中Html和图片文件
2、LoadData(loadDataWithBaseURL)显示APK中图片和文字混合的Html内容
res/layout/main.xml
Xml代码
< ?xml version="1.0" encoding="utf-8"?>
< LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
< WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" />
< /LINEARLAYOUT>
< ?xml version="1.0" encoding="utf-8"?>
< LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
< WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" />
< /LINEARLAYOUT>
Example_webview.java
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
Java代码
package cn.coolworks;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Example_webview extends Activity {
WebView webView;
final String mimeType = "text/html";
final String encoding = "utf-8";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//
//webHtml();
//
//webImage();
//
//localHtmlZh();
//
//localHtmlBlankSpace();
//
//localHtml();
//
// localImage();
//
localHtmlImage();
}
/**
* 直接网页显示
*/
private void webHtml() {
try {
webView.loadUrl("http://www.google.com");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 直接网络图片显示
*/
private void webImage() {
try {
webView
.loadUrl("http://www.gstatic.com/codesite/ph/images/code_small.png");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 中文显示
*/
private void localHtmlZh() {
try {
String data = "测试含有 中文的Html数据";
// utf-8编码处理(在SDK1.5模拟器和真实设备上都将出现乱码,SDK1.6上能正常显示)
//webView.loadData(data, mimeType, encoding);
// 对数据进行编码处理(SDK1.5版本)
webView.loadData(URLEncoder.encode(data, encoding), mimeType,
encoding);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 中文显示(空格的处理)
*/
private void localHtmlBlankSpace() {
try {
String data = " 测试含有空格的Html数据 ";
// 不对空格做处理
webView.loadData(URLEncoder.encode(data, encoding), mimeType,
encoding);
//webView.loadData(data, mimeType, encoding);
// 对空格做处理(在SDK1.5版本中)
webView.loadData(URLEncoder.encode(data, encoding).replaceAll(
"\+", " "), mimeType, encoding);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 显示本地图片文件
*/
private void localImage() {
try {
// 本地文件处理(如果文件名中有空格需要用+来替代)
webView.loadUrl("file:///android_asset/icon.png");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 显示本地网页文件
*/
private void localHtml() {
try {
// 本地文件处理(如果文件名中有空格需要用+来替代)
webView.loadUrl("file:///android_asset/test.html");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 显示本地图片和文字混合的Html内容
*/
private void localHtmlImage() {
try {
String data = "测试本地图片和文字混合显示,这是APK里的图片";
// SDK1.5本地文件处理(不能显示图片)
// webView.loadData(URLEncoder.encode(data, encoding), mimeType,
// encoding);
// SDK1.6及以后版本
// webView.loadData(data, mimeType, encoding);
// 本地文件处理(能显示图片)
webView.loadDataWithBaseURL("about:blank", data, mimeType,
encoding, "");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
这就是WebView组件的使用详解,如果您对WebView组件及Android开发有什么新的使用心得可以发Email:zhousn@51cto.com,51CTO将与您一起分享!
【编辑推荐】