2010-11-11 67 views
3

XMLAndroid:你如何在Tabview中顯示Webview?

<?xml version="1.0" encoding="utf-8"?> 
<WebView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myWebView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 

的Java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 
    WebView webView = (WebView) findViewById(R.id.myWebView); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.google.com"); 
} 

TabView的XML

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 

TabView的java的

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabview); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 


     intent = new Intent().setClass(this, WebActivity.class); 
     spec = tabHost.newTabSpec("webview") 
       .setIndicator("webview", res.getDrawable(R.drawable.info)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

       // add other tabs 

     tabHost.setCurrentTab(0); 
    } 

這將在全屏幕啓動網頁視圖。
是否可以在tabview中顯示webview?

回答

2

您可以在這裏找到答案:http://developer.android.com/guide/webapps/webview.html#HandlingNavigation

默認的WebView啓動瀏覽器每次你訪問一個新的URL,因此發生的事情是,它會啓動瀏覽器。

爲了避免這種情況發生,每次點擊的東西等等,你需要一個WebViewClient添加到您的WebView:

WebView myWebView = (WebView) findViewById(R.id.webview); 
myWebView.setWebViewClient(new WebViewClient()); 
myWebView.loadUrl("http://www.example.com"); 

如果您需要[執行當用戶點擊一個鏈接一些特定的動作,實現自己的WebViewClient :

public class MyWebViewClient extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     boolean result = false; 

     /* ... */ 
     // Return false to proceed loading page, true to interrupt loading 

     return result; 
    } 
} 

並使用它:

myWebView.setWebViewClient(new MyWebViewClient());