android - 如何使用fragments在一个屏幕上打开两个webview

标签 android android-fragments android-webview android-fragmentactivity

谁能帮我弄清楚如何使用 Android fragment 在同一屏幕上打开两个 webview,每个 webview 必须显示特定的网页,例如:1-google,2,yahoo。

我尝试了太多的教程和示例。对我来说没有任何效果...:(

对我来说,与我的想法相冲突的主要问题是,在 fragment 类中写什么来打开一个 webView 以及在运行整个应用程序的主要 Activity 中写什么。

在此先感谢您的帮助..:)

这是我的代码,在单屏纵向模式下运行良好,在横向模式下崩溃:

package com.example.androidwebviewfragment;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Fragment1 extends Fragment {

    WebView myWebView;
    final static String myBlogAddr = "http://android-er.blogspot.com";
    String myUrl;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_webfragment,container,false);
        myWebView = (WebView)view.findViewById(R.id.mywebview);

        myWebView.getSettings().setJavaScriptEnabled(true);                
        myWebView.setWebViewClient(new MyWebViewClient());

        if(myUrl == null){
            myUrl = myBlogAddr;
        }
        myWebView.loadUrl(myUrl);

        return view;

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            myUrl = url;
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}

这是第二个 fragment :

package com.example.androidwebviewfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class Fragment2 extends Fragment {

    WebView myWebView;
    final static String myBlogAddr = "http://android-er.blogspot.com";
    String myUrl;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view =   inflater.inflate
 (R.layout.layout_webfragment2,container,false);
        myWebView = (WebView)view.findViewById(R.id.mywebview);

        myWebView.getSettings().setJavaScriptEnabled(true);                
        myWebView.setWebViewClient(new MyWebViewClient());

        if(myUrl == null){
            myUrl = myBlogAddr;
        }
        myWebView.loadUrl(myUrl);

        return view;

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            myUrl = url;
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}

这是主要 Activity : 包 com.example.androidwebviewfragment;

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

public class MainActivity extends Activity {

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

}

the .xml files are :

1- fragment 1:

<WebView
    android:id="@+id/mywebview"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

</LinearLayout>

2- fragment 2 :

</LinearLayout>

3-主要的xml布局:

<fragment
    android:name="com.example.androidwebviewfragment.Fragment1"
    android:id="@+id/myweb_fragment1"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

  <fragment
    android:name="com.example.androidwebviewfragment.Fragment2"
    android:id="@+id/myweb_fragment2"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

 </RelativeLayout>

这是 layout-land 文件夹中的 main.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="horizontal" >
   <fragment
    android:name="com.example.androidwebviewfragment.Fragment1"
    android:id="@+id/myweb_fragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

  <fragment
    android:name="com.example.androidwebviewfragment.Fragment2"
    android:id="@+id/myweb_fragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent" /> 

 </LinearLayout>

日志中的横向模式错误:

java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.example.androidwebviewfragment                                                                                           
/com.example.androidwebviewfragment.MainActivity}
:android.view.InflateException: Binary XML file line #12: Error inflating class fragment

最佳答案

在 fragment 类中,您创建 View 并将其返回给主 Activity ,在主 Activity 中,您创建一个 fragment 适配器来绑定(bind) fragment 。有关更多详细信息,请参阅 ( http://developer.android.com/reference/android/support/v4/view/ViewPager.html) 链接并查看 (http://www.vogella.com/articles/AndroidFragments/article.html) 示例。

关于android - 如何使用fragments在一个屏幕上打开两个webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17906861/

相关文章:

java - 在 Fragments 的 ViewPager 中,Fragments 不能像卡片一样翻转

android - 从相机上传照片在 Nexus、Android WebView 中不起作用

android - Chrome Web View : triggering native Android Share function

android - 如何在android屏幕(webview)上适应网页?

android - 如何为我的 android 应用程序制作教程?

android - 如何在不滚动android的情况下将折叠工具栏显示为折叠

android - android studio 中的多语言支持

java - 如何确保 fragment 是通过静态方法 new Instance 在 Activity 中创建的

android - 在 2 个以上的 fragment 之间进行通信

java - Android - 以线性布局显示 WebView