带有子RelativeLayout的ScrollView不会滚动

时间:2015-04-11 15:26:33

标签: android android-layout android-scrollview android-scroll

我有一个ScrollView,其中包含1个RelativeLayout,其中有3个RelativeLayouts

enter image description here

我将RelativeLayout设置为alignParentTop="true"

底部RelativeLayoutalignParentBottom="true"

中间布局

android:layout_below="@id/topLayout"
android:layout_above="@+id/bottomLayout" 

问题:当屏幕很小时,Scrollview不会滚动,而topLayout会保持在最顶层,而bottomLayout会保持在底部。中间布局变小,甚至丢失,如下所示:

enter image description here

渴望:我希望topLayout保持在最高点和最低点,保持在最低点。但是当没有空间时,必须滚动,以便中间布局不会丢失。

代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:paddingBottom="@dimen/halfClassic"
        android:paddingLeft="@dimen/halfClassic"
        android:paddingRight="@dimen/halfClassic"
        android:paddingTop="@dimen/halfClassic">

        <RelativeLayout
            android:id="@+id/layoutWeekChoice"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:padding="0dp"
            android:background="@color/white"
            android:layout_alignParentTop="true">

            <Spinner
                android:id="@+id/spinnerWeekChoice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minHeight="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/buttonSetDate"
                android:layout_toStartOf="@+id/buttonSetDate"
                />
            <Button
                android:id="@+id/buttonSetDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:maxWidth="150dp"
                android:textSize="16sp"
                android:text="Set Date"
                android:gravity="center"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:background="@drawable/button_background"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"
                android:layout_alignBottom="@+id/spinnerWeekChoice"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutCardRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center"
            android:layout_marginTop="32dp"
            android:layout_below="@id/layoutWeekChoice"
            android:layout_above="@+id/layoutShowAttendance"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageCard"
                android:src="@drawable/std_card"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="16dp"
                android:layout_centerHorizontal="true"/>

            <TextView
                android:id="@+id/textWarningCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Checking..."
                android:textColor="@color/black"
                android:textSize="14sp"
                android:layout_below="@+id/imageCard"
                android:layout_centerHorizontal="true"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutShowAttendance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/halfClassic"
            android:background="@color/white"
            android:layout_alignParentBottom="true">

            <TextView
                android:id="@+id/textAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Attended: "
                android:textColor="@color/black"
                android:layout_alignParentTop="true"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/textNotAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Not Attended: "
                android:textColor="@color/black"
                android:textSize="16sp"
                android:layout_marginBottom="32dp"
                android:layout_below="@+id/textAttended"/>

            <Button
                android:id="@+id/buttonManualAttendance"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_height="wrap_content"
                android:text="Manual Attendance"
                android:gravity="center"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:background="@drawable/button_background"
                android:minHeight="50dp"
                android:layout_below="@id/textNotAttended"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"/>

        </RelativeLayout>


    </RelativeLayout>
</ScrollView>

注意:我不想将LinearLayoutweight="1"一起使用,这将使我的3个布局高度相同

编辑:现在ScollView滚动下面有新代码但我的底部TextViews迷路了:

enter image description here

代码已更新:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context="com.jemshit.itu.fragments.TakeAttendanceFragment"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:paddingBottom="@dimen/halfClassic"
        android:paddingLeft="@dimen/halfClassic"
        android:paddingRight="@dimen/halfClassic"
        android:paddingTop="@dimen/halfClassic">

        <RelativeLayout
            android:id="@+id/layoutWeekChoice"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:padding="0dp"
            android:background="@color/white"
            android:layout_alignParentTop="true">

            <Spinner
                android:id="@+id/spinnerWeekChoice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minHeight="50dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_toLeftOf="@+id/buttonSetDate"
                android:layout_toStartOf="@+id/buttonSetDate"
                />
            <Button
                android:id="@+id/buttonSetDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:maxWidth="150dp"
                android:textSize="16sp"
                android:text="Set Date"
                android:gravity="center"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:background="@drawable/button_background"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"
                android:layout_alignBottom="@+id/spinnerWeekChoice"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutCardRead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:gravity="center"
            android:layout_marginTop="32dp"
            android:layout_below="@id/layoutWeekChoice"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageCard"
                android:src="@drawable/std_card"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="16dp"
                android:layout_centerHorizontal="true"/>

            <TextView
                android:id="@+id/textWarningCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Checking..."
                android:textColor="@color/black"
                android:textSize="14sp"
                android:layout_below="@+id/imageCard"
                android:layout_centerHorizontal="true"
                />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutShowAttendance"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/halfClassic"
            android:background="@color/white"
            android:layout_below="@id/layoutCardRead"
            android:layout_alignParentBottom="true">

            <TextView
                android:id="@+id/textAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Attended: "
                android:textColor="@color/black"
                android:layout_above="@+id/textNotAttended"
                android:textSize="16sp"/>
            <TextView
                android:id="@+id/textNotAttended"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Not Attended: "
                android:textColor="@color/black"
                android:textSize="16sp"
                android:layout_marginBottom="32dp"
                android:layout_above="@+id/buttonManualAttendance" />

            <Button
                android:id="@+id/buttonManualAttendance"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_height="wrap_content"
                android:text="Manual Attendance"
                android:gravity="center"
                android:textAllCaps="false"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:background="@drawable/button_background"
                android:minHeight="50dp"
                android:layout_alignParentBottom="true"
                android:paddingLeft="@dimen/halfClassic"
                android:paddingRight="@dimen/halfClassic"/>

        </RelativeLayout>


    </RelativeLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:1)

将您的父母RelativeLayout身高设为wrap_contentMatch_parent只需要ScrollView,其中的所有内容都可以占用所需的所有空间。

答案 1 :(得分:1)

从您的第二个相对布局,即布局卡删除android:layout_above="@+id/layoutShowAttendance"

并从layoutShowAttendance中删除 android:layout_alignParentBottom="true" 并将android:layout_below="@+id/layoutCardRead"添加到layoutShowAttendance

它会起作用。