如何在 Android Activity 中使可滚动布局紧随其后的是另一个布局

问题描述 投票:0回答:1

这是我想在 Android 活动中获得的内容:

+------------------++------------------++------------------+
| +--------------+ || +--------------+ || +--------------+ |
| |    Table     | || |    Table     | || |    Table    #| |
| |              | || |              | || |             #| |
| |              | || |              | || |             #| |
| |              | || |              | || |             #| |
| +--------------+ || |              | || |             #| |
| +--------------+ || |              | || |             #| |
| | Linearlayout | || |              | || |             #| |
| +--------------+ || +--------------+ || |              | |
|                  || +--------------+ || |              | |
|                  || | Linearlayout | || +--------------+ |
|                  || +--------------+ || +--------------+ |
|                  ||                  || | Linearlayout | |
|                  ||                  || +--------------+ |
|  +------------+  ||  +------------+  ||  +------------+  |
|  |   Button   |  ||  |   Button   |  ||  |   Button   |  |
|  +------------+  ||  +------------+  ||  +------------+  |
+------------------++------------------++------------------+

目标:

  • 我有一个可变长度的表。
  • 表格无法完全显示时应可滚动
  • 我希望 Linearlayout 始终位于表格旁边
  • 按钮应始终位于底部

我尝试了 ConstraintLayout 和 Linearlayout 的多种组合,但都没有成功:

  • 或者当表格太长时Linearlayout消失
  • 或者 Linearlayout 保持靠近按钮。

这是一个不起作用的代码示例(简化):

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="0dp"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent"
                  app:layout_constraintBottom_toTopOf="@id/B2"
                  android:orientation="vertical">
        <ScrollView android:id="@+id/S1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:layout_weight="0">
            <TableLayout android:id="@+id/table"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content">
                <TableRow >...</TableRow>
                <TableRow >...</TableRow>
                <TableRow >...</TableRow>
            </TableLayout>
        </ScrollView>
        <LinearLayout>.....</LinearLayout>
    </LinearLayout>
    <Button android:id="@+id/B2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="validate"
            app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我知道我可以通过使用 OnPreDrawListener 来实现这一点,但我更喜欢更静态的东西,只需使用 xml 中的布局。

我怎样才能实现这个目标?

android android-layout layout
1个回答
0
投票

这应该有效,至少在预览中是这样: 我希望我正确理解了您的要求。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_max="wrap"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/button">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/green_like">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="100dp"/>

            </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/red_400">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"/>
            </TableRow>
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/green_like">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"/>

            </TableRow>
        </TableLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/primary"
        android:orientation="vertical"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintTop_toBottomOf="@id/scrollView"
        app:layout_constraintBottom_toTopOf="@id/button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="validate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

LinearLayout 为蓝色,表格单元格为绿色和红色

© www.soinside.com 2019 - 2024. All rights reserved.