【问题标题】:ScrollView with sub RelativeLayout does not scroll带有子 RelativeLayout 的 ScrollView 不滚动
【发布时间】:2015-04-11 15:26:32
【问题描述】:

我有一个 ScrollView 和 1 个 RelativeLayout,其中有 3 个子 RelativeLayouts

我将顶部RelativeLayout 设置为alignParentTop="true"

底部RelativeLayoutalignParentBottom="true"

中间布局到

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

问题: Scrollview 在小屏幕时不滚动,而是 topLayout 停留在顶部,bottomLayout 停留在底部。中间的布局变小了(甚至丢失了),如下所示:

期望:我希望 topLayout 留在顶部,bottomLayout 留在底部。但是没有空间的时候一定要滚动,所以中间的布局不能丢失。

代码:

<?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 迷路了:

代码更新:

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

【问题讨论】:

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


    【解决方案1】:

    将您的父母RelativeLayout 高度设置为wrap_contentMatch_parent只需要ScrollView,里面的所有东西都可以用尽它所需要的空间。

    【讨论】:

    • 刚刚注意到您将belowabove 属性设置为中间布局。仅对中间和底部布局使用 below 属性。
    • yes 确实可以滚动,但我在 bottomLayout 中的文本在滚动时会丢失。我有 2 个文本和 1 个按钮。按钮设置为 alignParentBottom,其他 2 个文本设置为 layout_above="button" 和 layout_above="text2"。 :postimg.org/image/mt186ftfl可能是什么问题
    • 不需要alignParentBottom - 布局将拉伸以包含全尺寸文本和按钮。只需将带有alignParentTop 的顶部文本和带有above 属性的其他视图放在一起。如果您通过编写已有的代码来编辑您的问题,那就太好了。
    • 我可以这样做,但这样我的底部文本和按钮将位于 bottomLayout 的顶部,并且屏幕底部会有空白
    • 您可以将父级RelativeLayout 替换为垂直方向的LinearLayout,并为中间布局设置weight=1 属性。当然,那样below属性就没用了。
    【解决方案2】:

    从您的第二个相对布局中,即 layoutcard remove android:layout_above="@+id/layoutShowAttendance"

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

    它会起作用的。

    【讨论】:

    • 但是我的底部布局不会在屏幕底部,屏幕底部会有空白,对吧?
    • 如果你在更大的屏幕上运行它,底部会有一个空白。但是如果您将布局设置在底部,它将不会滚动,如果您设置更大的图像,它甚至会与您的图像重叠。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    相关资源
    最近更新 更多