在一个屏幕中显示两个webview

时间:2012-11-14 11:09:39

标签: android-webview

我正在尝试制作一个可以在一个屏幕上显示两个网站的webview应用程序。但是当我运行我的应用程序时,它显示一个网站并在新浏览器上打开。我如何显示两个网站,它不适用于新的浏览器? 这是我的WebViewActivity类:

    package com.mkyong.android;

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;

   public class WebViewActivity extends Activity {

    private WebView webView1, webView2;

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

        webView1 = (WebView) findViewById(R.id.webView1);
        webView1.getSettings().setJavaScriptEnabled(true);
        webView1.loadUrl("http://www.google.com");
        webView2 = (WebView) findViewById(R.id.webView2);
        webView2.getSettings().setJavaScriptEnabled(true);
        webView2.loadUrl("http://www.google.com");



    }

 }

这是我的xml文件:

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <WebView
        android:id="@+id/webview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.39" />


    <WebView
        android:id="@+id/webview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.53" />

  </LinearLayout>

1 个答案:

答案 0 :(得分:0)

我建议您使用framelayout并尝试将webviewClient设置为您的webview

此函数shouldOverrideUrlLoading(在webviewClient的声明中)

@Override
             public boolean shouldOverrideUrlLoading(WebView view, String url) 
            {
                Log.i("Page", url);
                view.setWebViewClient(new MyWebViewClient());
                view.setWebChromeClient(new MyWebChromeClient());
                view.loadUrl(url);
                return true;
            }

将覆盖webview加载的url并强制将其加载到webview中,而不是在新浏览器中加载