0

我得到了例外:

java.lang.IllegalStateException: ScrollView can host only one direct child

这是我的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bg_listgrey"
    android:scrollbars="none" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/bg_listgrey" >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/rl1">

            <ImageView
               />

            <TextView
                />

            <TextView
               />

            <TextView
                />
        </RelativeLayout>

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/rl2"" >

            <ImageView
                 />

            <TextView
                 />
        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

在 Activity 的情况下,相同的布局可以正常工作。因为它在Fragment中使用时会给出异常。

主要活动:

FragmentTransaction t = this.getSupportFragmentManager()
        .beginTransaction();
t.add(R.id.frame, new mFragment());
t.commit();

谢谢

编辑:

如果我将此 ScrollView 包装在 FragmeLayout 中,它可以正常工作。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/bg_listgrey" >

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none" >
.......
.......
.......
</ScrollView>
</FramLayout>
4

1 回答 1

1

在 Activity 的情况下,相同的布局可以正常工作。在片段中使用时它会给出异常。

布局将作为 an 的内容视图正常工作Activity(但尝试向 the 添加另一个视图ScrollView并查看它的运行情况;))并且它也适用于 a Fragment,只是你没有很好地使用它。这:

t.add(R.id.frame, new mFragment());

会将Fragment's视图(在 中创建的onCreate)添加到ScrollViewScrollView具有 id 的R.id.frame),这意味着您将在ScrollView(不允许的)中有两个视图:RelativeLayout和视图的根Fragment。该类的addView方法ScrollView将检查以强制您最终在ScrollView.

如果我将此 ScrollView 包装在 FragmeLayout 中,它可以正常工作。

这是正常的,因为您将Fragment的视图添加到父级FrameLayout而不是ScrollView.

于 2012-12-09T15:21:30.250 回答