【问题标题】:Can you have two webViews running at the same time in the same activity in Android?您可以在 Android 的同一个活动中同时运行两个 webView 吗?
【发布时间】:2014-01-29 19:57:49
【问题描述】:

我创建了一个简单的活动,其中包含两个并排的 webView。我使用以下代码来启动它们。

setContentView(R.layout.detail); //xml layout that contains two webviews

webView1 = (WebView) this.findViewById(R.id.webView1);
webView2 = (WebView) this.findViewById(R.id.webView2);

webView1.setWebChromeClient(new WebChromeClient());
webView1.loadUrl("http://www.google.com");


webView2.setWebChromeClient(new WebChromeClient());
webView2.loadUrl("http://www.google.com");

问题是上面的代码 webView1 加载正常,而 webView2 总是空白(只是一个白框),我不明白为什么。

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip" >


    <WebView
    android:id="@+id/webView2"
    android:layout_width="470dp"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true" />

    <WebView
    android:id="@+id/webView1"
    android:layout_width="470dp"
    android:layout_height="300dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"/>


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/webView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="49dp"
        android:text="Cell"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/TextView01"
        android:layout_alignParentLeft="true"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@+id/textView2"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/TextView01"
        android:layout_alignLeft="@+id/TextView01"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/webView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="23dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout> 

【问题讨论】:

  • 他们不是在彼此之上吗?你的屏幕有多大?
  • 不是nexus 7,它们是并排的,屏幕宽度大于1000dp。
  • 如果在 webView1 中不加载 url 会发生什么?
  • 你的两个 WebView 在什么类型的 ViewGroup 中?
  • 没有变化,我开始怀疑这是否只是 eclipse 的问题,我的 xml 文件。

标签: android android-webview webchromeclient


【解决方案1】:

我相信问题实际上出在 eclipse 上,因为在完全删除 webView1 后我收到了这个错误:

android.widget.TextView cannot be cast to android.webkit.WebView

你可以从我的代码中看到没有任何意义,因为你可以看到我没有在任何地方这样做,实际上甚至还没有使用 TextViews。

一个完整的干净和奇怪的问题仍然存在。我打算转到 Android Studio。

  • 移至 Android Studio,一切正常,处理布局比我使用 Eclipse 的体验快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    相关资源
    最近更新 更多