簡體   English   中英

Android:如何在Tabview中顯示Webview?

[英]Android: How do you display Webview inside Tabview?

XML

<?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);
    }

這將以全屏方式啟動webView。
是否可以在tabview中顯示webview?

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

默認情況下,每次訪問新URL時,WebView都會啟動瀏覽器,因此會啟動瀏覽器。

為了避免每次點擊某些內容時發生這種情況,您需要將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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM