WebView視圖二:加載HTML代碼

AndroidManifest.xml文件中加入網絡訪問的權限設置:html

    <uses-permission android:name="android.permission.INTERNET" />java

activity_main.xml代碼android

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jt.http_01.MainActivity" >
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>

WebViewLoadData.java代碼web

 /**
 * 使用WebView加載 HTML 代碼   
 * @author jiatao  
 * @date 2015-5-2  
 * @version 1.0
 */
package com.jt.http_01;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewLoadData extends Activity {
 private WebView webView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initDisplay();
 }
 public void initDisplay() {
  // TODO Auto-generated method stub
  webView = (WebView) findViewById(R.id.webView1);
  String htmlCode = "<html>"
    + "<head>"
    + "<title>Welcome</title>"
    + "</head>"
    + "<body>"
    + "<h2>Welcome to <a href=\"http://www.163.com\">歡迎來到網易</a></h2>"
    + "</body>" + "</html>";
  
  /**
   * 調用loadData()方法加載HTML代碼
   * public void loadData(String data, String mimeType, String encoding) { }
   */
  webView.loadData(htmlCode, "text/html;utf-8", "utf-8"); 
 }
}
相關文章
相關標籤/搜索