Android开发之WebView组件的使用详解

    转自:http://developer.51cto.com/art/201008/216488.htm
    本文希望通过本次对WebView组件的使用讲解,可以让各位了解到WebView组件的详细使用方法。

 

51CTO曾经独家推荐过Android开发应用详解的专题,本文希望通过本次对WebView组件的使用讲解,可以让各位了解到WebView组件的详细使用:

网络内容

1、LoadUrl直接显示网页内容(单独显示网络图片)

2、LoadData显示中文网页内容(含空格的处理)

APK包内文件

1、LoadUrl显示APK中Html和图片文件

2、LoadData(loadDataWithBaseURL)显示APK中图片和文字混合的Html内容

res/layout/main.xml

Xml代码

  1. < ?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  4.  
  5. < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" /> 
  6.  
  7. < /LINEARLAYOUT> 
  8.  
  9. < ?xml version="1.0" encoding="utf-8"?> 
  10.  
  11. < LINEARLAYOUT android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> 
  12.  
  13. < WEBVIEW android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" /> 
  14.  
  15. < /LINEARLAYOUT> 
  16.  
  17. Example_webview.java 

Java代码

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

猜你喜欢

转载自cshbbrain.iteye.com/blog/1846145