Android:如何在TabView内显示Webview?

3

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

标签视图 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>

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?

1个回答

2
您可以在此处找到答案:http://developer.android.com/guide/webapps/webview.html#HandlingNavigation 默认情况下,WebView每次访问新的URL时都会启动浏览器,所以会启动浏览器。
为了避免每次单击等操作时都发生这种情况,您需要向WebView添加一个WebViewClient:
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());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接