【问题标题】:Error :Exception raised during rendering: ScrollView can host only one direct child错误:渲染期间引发异常:ScrollView 只能承载一个直接子级
【发布时间】:2015-05-29 00:33:03
【问题描述】:

我正在尝试设计一个应用程序,但是当我在整个正文中放置一个 ScrollView 时说

渲染期间引发异常:ScrollView 只能承载一个直接子级

我做错了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/root">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/toolbar"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#303F9F"
        android:id="@+id/layout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:gravity="center"
            android:hint="Title"
            android:textSize="23dp"
            android:text="@string/contenido"
            android:textColor="#fff"
            android:layout_gravity="center"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:layout_marginTop="140dp"/>

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/view_include_footer"
            android:layout_alignParentBottom="true"
            />


    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/comment_rv"
            android:layout_below="@+id/setting_toolbar"
            android:scrollbars="vertical"
            android:background="#ccffffff"
            />
    </RelativeLayout>
    </ScrollView>

</LinearLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    ScrollView 只能承载一个直接子元素,这意味着您可以直接在其中使用一个元素。创建一个RelativeLayout 或最适合您的,并将您的include 和RelativeLayouts 都放在其中,并将此布局放在ScrollView 中。只有这样,您才会有一个直系子女。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/root">
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/toolbar"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/title"
        android:layout_alignStart="@+id/title" />
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:background="#303F9F"
        android:id="@+id/layout">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:gravity="center"
            android:hint="Title"
            android:textSize="23dp"
            android:text="@string/contenido"
            android:textColor="#fff"
            android:layout_gravity="center"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:layout_marginTop="140dp"/>
    
        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/view_include_footer"
            android:layout_alignParentBottom="true"
            />
    
    
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
    
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/comment_rv"
            android:layout_below="@+id/setting_toolbar"
            android:scrollbars="vertical"
            android:background="#ccffffff"
            />
    </RelativeLayout>
    </RelativeLayout>
    </ScrollView>
    

    【讨论】:

    • 感谢您按照我所说的方式回答 Vastal,但有一个问题是属性 android: layout_height = "match_parent" RecyclerView 没有被正确实施,因为当我尝试显示时日志只显示一条两条记录因为它发生的问题是有 ScrollView
    【解决方案2】:

    我之前遇到过这个问题,很明显 ScrollView 只能承载一个直接子级。这意味着你不应该使用两个! ScrollView中的子视图,只是在两个子视图的外层包围了一个RelativeLayout。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      • 2011-09-25
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多