網絡視圖(WebView)是一個顯示網頁的視圖(View). 通過使用網絡視圖(WebView), 您可以把網絡瀏覽器顯示在您的活動(Activity)上.

Android Browser using WebView

注意,為了使您的活動(Activity)接入互聯網並從網絡視圖(WebView)加載網頁, 您必須在清單文件(AndroidManifest.xml)裡, 的子項中添加"android.permission.INTERNET"這個權限.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.AndroidWebView"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".AndroidWebView"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.   
  15.     </application>  
  16.     <uses-sdk android:minSdkVersion="5" />  
  17.     <uses-permission android:name="android.permission.INTERNET" />  
  18. </manifest>   



修改main.xml, 添加一個WebView.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <WebView android:id="@+id/mybrowser"   
  8.    android:layout_width="fill_parent"   
  9.    android:layout_height="fill_parent"   
  10.    />  
  11. </LinearLayout>  



修改源碼

  1. package com.AndroidWebView;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.webkit.WebSettings;  
  6. import android.webkit.WebView;  
  7. import android.webkit.WebViewClient;  
  8.   
  9. public class AndroidWebView extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.   
  15.         setContentView(R.layout.main);  
  16.   
  17.         String myURL = "http://www.google.com/m/";         
  18.         WebView myBrowser=(WebView)findViewById(R.id.mybrowser);  
  19.   
  20.         WebSettings websettings = myBrowser.getSettings();  
  21.         websettings.setSupportZoom(true);  
  22.         websettings.setBuiltInZoomControls(true);   
  23.         websettings.setJavaScriptEnabled(true);  
  24.          
  25.         myBrowser.setWebViewClient(new WebViewClient());  
  26.   
  27.         myBrowser.loadUrl(myURL);  
  28.           
  29.     }  
  30. }  



默認的設置是限制JavaScript, 所以需要使用此代碼啟用JavaScript:
websettings.setJavaScriptEnabled(true);

下面兩句代碼非常明顯, 啟用內置的縮放功能. 當網頁滾動時, 內置的縮放功能(+/-符號)便會出現.
websettings.setSupportZoom(true);
websettings.setBuiltInZoomControls(true); 

在默認設定之下, 如果你點擊一個鏈接, 它會調用Android自帶的瀏覽器打開鏈接. 如果你想繼續使用你自己的瀏覽器, 可以使用下面的代碼.
myBrowser.setWebViewClient(new WebViewClient());

arrow
arrow
    全站熱搜

    Felix 發表在 痞客邦 留言(0) 人氣()