【问题标题】:implement scrollView in form在表单中实现scrollView
【发布时间】:2016-06-09 10:05:05
【问题描述】:

我正在尝试在我的表单中添加ScrollView。但是每当我添加ScrollView 时,它都会显示ScrollView 只能容纳一个直接的孩子”。这是什么意思?如何在我的表单中实现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 中必须有唯一的孩子意味着&lt;ScrollView&gt;--&lt;LinearLayout&gt;--All Other layouts--&lt;/LinearLayout&gt;&lt;/ScrollView&gt;
  • ScrollView 里面只能有一个元素。将其他元素包含在线性布局中。
  • 你们没有谷歌??
  • 谢谢阿布舍克·帕特尔 :)
  • @SamiurRahman 总是厕所

标签: android xml scrollview


【解决方案1】:

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

这个孩子本身可能是一个具有对象层次结构的布局管理器。一个经常使用的子元素是一个垂直方向的LinearLayout,表示用户可以滚动浏览的顶级项目的垂直数组。

这样试试

<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>

【讨论】:

  • 谢谢我知道它会像我现在理解的魅力一样工作...... :)
【解决方案2】:

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

<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>

【讨论】:

  • 不要在滚动视图中使用多个布局
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2016-11-03
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多