Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将webview嵌入到另一个视图中_Android_Android Webview - Fatal编程技术网

Android 将webview嵌入到另一个视图中

Android 将webview嵌入到另一个视图中,android,android-webview,Android,Android Webview,我的应用程序中有2个视图: a。res/layout/main.xml-带有1个按钮的标准视图 b。res/layout/web_view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_w

我的应用程序中有2个视图:

a。res/layout/main.xml-带有1个按钮的标准视图

b。res/layout/web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
         <WebView android:id="@+id/webview"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent" />
</LinearLayout>
这一切都很好,url加载很好,但是浏览器被实例化到它自己的视图中(第三个视图,不是b本身),我的目标是使用Webview在我的应用程序中显示一些HTML代码,而不是在它之外,在一个单独的浏览器中

有什么想法吗

这是使用API level8/Android 2.2完成的

谢谢你的帮助。
保罗实际上我终于明白了。即使您以编程方式加载url

mWebView.loadUrl("http://www.google.com");
您还必须修改默认行为(即在新浏览器实例中打开url)。
前面的代码需要2个增强

// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }  
然后,为使用Webclient的视图设置新行为:

public void goToWebView(View view) {
        setContentView(R.layout.web_view);
        WebView mWebView = (WebView) findViewById(R.id.webview);
        // add the following line ----------
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
    }

是的,就是这样。你的问题实际上很有说服力。你的问题应该是关于在webviewThank@Paul中处理重定向的。我终于找到了我两个小时来寻找的东西。嗨,我只是在找这个。谢谢你的回答,我想知道你是怎么知道这件事的?我的意思是你读过Android代码中的每一个类吗?嗨@Harsha。老实说,我记不清了。我想我重新检查了文档和一些示例,并从中找到了答案。谢谢,所以基本上我现在需要好好学习android,并对它充满信心。所以我正在寻找实现这一点的方法。只有当我打开一些URL时,才会发生这种情况,而intent侦听器是为这些URL注册的。例如,google chrome似乎注册为打开
http://www.google.com
。当我尝试任何其他域
http://www.something.com
它将在同一视图中打开。仅供参考
public void goToWebView(View view) {
        setContentView(R.layout.web_view);
        WebView mWebView = (WebView) findViewById(R.id.webview);
        // add the following line ----------
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
    }