中间需要一个页眉、页脚和 ScrollView。当前代码不起作用

The*_*bah 4 android header footer scrollview

在过去的一个小时里,我一直在搜索这里和其他网站,试图找出我做错了什么。正如标题所示,我需要一个带按钮的页眉、一个滚动视图,然后是一个带按钮的页脚。我希望页眉和页脚始终可见。找到的所有内容都表示要创建一个相对布局,将其与父顶部对齐,创建页脚,将其与父底部对齐,然后创建一个滚动视图,将其设置在页脚上方和标题下方。由于某种原因,我的代码决定忽略这一点。

<RelativeLayout
    android:id="@+id/topBar_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorBackground">

    <Button
        android:id="@+id/charInfoBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="General"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</RelativeLayout>

<ScrollView
    android:id="@+id/genLayoutScroll_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/bottomBar_id"
    android:layout_below="@id/topBar_id>

    <LinearLayout
      //bunch of junk that users scroll through
    </LinearLayout>

</ScrollView>

<RelativeLayout
    android:id="@+id/bottomBar_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:background="@color/colorBackground">

    <Button
        android:id="@+id/genApplyBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

使用它作为我当前的代码,滚动视图位于顶部栏和底部栏的顶部。

如果我删除layout_above和layout_below行,并将其替换为app:layout_constraintBottom_toTopOf =“@id/bottomBar_id”,那么滚动视图不再越过我的底部栏,但现在顶部栏和滚动视图的顶部丢失了。

然后我将 app:layout_constraintTop_toBottomOf="@id/topBar_id" 添加到滚动视图,它会返回到与具有layout_below 和layout_above 相同的状态。

我确信我在这里错过了一些东西,我只是不知道我错过了什么。希望有人能指出我犯的愚蠢错误。

PS我知道我的一些代码现在有点草率,主要是因为试图解决这个问题。

Ben*_* P. 5

到目前为止,最简单的方法是使用垂直线LinearLayout作为视图的根,并使用“布局权重”的概念使中间元素( )ScrollView填充屏幕。

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

    <Button
        android:id="@+id/charInfoBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="General"/>

    <ScrollView
        android:id="@+id/genLayoutScroll_id"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- your contents here -->

    </ScrollView>

    <Button
        android:id="@+id/genApplyBtn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果每个栏中需要多个按钮,可以将它们包装在水平LinearLayout.

其起作用的原因是标签android:layout_weight="1"上的属性<ScrollView>。在其他视图占用它们所需的空间后,将权重分配给单个视图将使其占用所有剩余空间。因此,您的按钮可以获得所需的空间,而滚动视图则获得剩余的空间。由于它位于中间,这必然会将底部按钮向下推到屏幕的末端。