在表单中实现 scrollView

implement scrollView in form

我正在尝试在我的表单中添加 ScrollView。但是每当我添加 ScrollView 时,它都会显示 "ScrollView can host only an direct child"。这是什么意思?我如何实现 ScrollView 在我的形式?这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

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

    <ImageView
        android:id="@+id/showImage"
        android:layout_width="400dp"
        android:layout_height="100dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />
    <!--android:src="@drawable/placeholder"-->
    <Button
        android:id="@+id/btnSaveImage"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal"
        android:background="#f66565"
        android:text="Select Image" />

    <EditText
        android:id="@+id/editFirstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Blog Title">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editLastName"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:ems="10"
        android:hint="Enter Blog Description" />

    <EditText
        android:id="@+id/editWeb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter Web Link">

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp">

        <Button
            android:id="@+id/btnNewUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:background="#f66565"
            android:text="New Blog" />

        <Button
            android:id="@+id/btnSaveRecord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:background="#f66565"
            android:text="Save Blog" />

        <Button
            android:id="@+id/btnfullinfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:background="#f66565"
            android:text="Show All" />

    </LinearLayout>
    </ScrollView>

</LinearLayout>

一个ScrollView 只能承载一个直接child,这意味着您应该在其中放置一个child,其中包含要滚动的全部内容。

这个 child 本身可能是一个层次结构为 objects 的布局管理器。经常使用的 child 是垂直方向的 LinearLayout,呈现一个垂直排列的 top-level 项目,用户可以滚动浏览。

像这样尝试

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

  <LinearLayout android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

       // all other views currently in your ScrollView
 </LinearLayout>

</ScrollView>

滚动视图只能包含一个子视图。所以你应该使用顶视图作为滚动视图,并根据需要将子视图设为线性或相对或其他。然后在该线性或相对布局中添加所有其他视图,如按钮或文本视图。您不能在滚动视图中添加更多子视图。这里你的代码应该是这样的:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <ImageView
            android:id="@+id/showImage"
            android:layout_width="400dp"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter" />
        <!--android:src="@drawable/placeholder"-->
        <Button
            android:id="@+id/btnSaveImage"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_gravity="center_horizontal"
            android:background="#f66565"
            android:text="Select Image" />

        <EditText
            android:id="@+id/editFirstName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Blog Title">

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editLastName"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:ems="10"
            android:hint="Enter Blog Description" />

       <EditText
            android:id="@+id/editWeb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ems="10"
            android:hint="Enter Web Link">

            <requestFocus />
       </EditText>

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="20dp">

            <Button
                android:id="@+id/btnNewUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="New Blog" />

           <Button
                android:id="@+id/btnSaveRecord"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="Save Blog" />

           <Button
                android:id="@+id/btnfullinfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:background="#f66565"
                android:text="Show All" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>