自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android開發(fā)之WebView組件的使用詳解

移動(dòng)開發(fā) Android
本文希望通過本次對(duì)WebView組件的使用講解,可以讓各位了解到WebView組件的詳細(xì)使用方法。

51CTO曾經(jīng)獨(dú)家推薦過Android開發(fā)應(yīng)用詳解的專題,本文希望通過本次對(duì)WebView組件的使用講解,可以讓各位了解到WebView組件的詳細(xì)使用:

網(wǎng)絡(luò)內(nèi)容

1、LoadUrl直接顯示網(wǎng)頁(yè)內(nèi)容(單獨(dú)顯示網(wǎng)絡(luò)圖片)

2、LoadData顯示中文網(wǎng)頁(yè)內(nèi)容(含空格的處理)

APK包內(nèi)文件

1、LoadUrl顯示APK中Html和圖片文件

2、LoadData(loadDataWithBaseURL)顯示APK中圖片和文字混合的Html內(nèi)容

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代碼

  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. * 直接網(wǎng)頁(yè)顯示  
  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.   * 直接網(wǎng)絡(luò)圖片顯示  
  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 = "測(cè)試含有 中文的Html數(shù)據(jù)";  
  116.  
  117.   // utf-8編碼處理(在SDK1.5模擬器和真實(shí)設(shè)備上都將出現(xiàn)亂碼,SDK1.6上能正常顯示)  
  118.  
  119.   //webView.loadData(data, mimeType, encoding);  
  120.  
  121.   // 對(duì)數(shù)據(jù)進(jìn)行編碼處理(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 = " 測(cè)試含有空格的Html數(shù)據(jù) ";  
  146.  
  147.   // 不對(duì)空格做處理  
  148.  
  149.   webView.loadData(URLEncoder.encode(data, encoding), mimeType,  
  150.  
  151.   encoding);  
  152.  
  153.   //webView.loadData(data, mimeType, encoding);  
  154.  
  155.   // 對(duì)空格做處理(在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.   * 顯示本地網(wǎng)頁(yè)文件  
  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內(nèi)容  
  216.  
  217.   */  
  218.  
  219.   private void localHtmlImage() {  
  220.  
  221.   try {  
  222.  
  223.   String data = "測(cè)試本地圖片和文字混合顯示,這是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.  
  249.   } 

 

  1.   

這就是WebView組件的使用詳解,如果您對(duì)WebView組件及Android開發(fā)有什么新的使用心得可以發(fā)Email:zhousn@51cto.com,51CTO將與您一起分享!

【編輯推薦】

  1. ******的Android開發(fā)類說明 
  2. 對(duì)Android開發(fā)技巧進(jìn)行全解析 
  3. 闡述Android開發(fā)過程中的難題 
  4. 探索***的Android開發(fā)環(huán)境內(nèi)容 
  5. Android開發(fā)中的窗口小部件App Widgets 
責(zé)任編輯:佚名 來源: cnmsdn
相關(guān)推薦

2010-02-03 15:59:08

Android組件

2012-12-26 12:41:14

Android開發(fā)WebView

2016-10-24 14:04:24

2015-03-03 15:53:31

Android控件

2013-09-13 13:15:28

AndroidWebViewJavaScript

2011-04-12 08:40:23

IMFAndroid

2013-01-06 12:23:59

Android開發(fā)SQLite數(shù)據(jù)庫(kù)

2011-07-18 14:39:53

iPhone SDK UIKit

2011-09-13 17:03:16

Eclipse And

2009-09-16 13:05:32

C#組件開發(fā)

2009-04-10 09:06:16

Windows Emb

2013-12-25 09:34:26

Android SDKAndroid組件

2013-01-10 14:21:24

Android開發(fā)組件Activities

2014-12-17 11:09:39

Hybrid AppWebView裝載頁(yè)面

2021-12-20 10:07:39

鴻蒙HarmonyOS應(yīng)用

2011-08-02 11:07:42

iOS開發(fā) UIWebView

2013-05-20 17:33:44

Android游戲開發(fā)自定義View

2011-02-28 13:04:27

RelativeLayAndroid Wid

2017-07-14 09:29:45

AndroidWebview

2010-07-13 09:02:19

Widget開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)