首页 > 解决方案 > 如何使 ConstraintLayout 中的 View 填充可用高度?

问题描述

我是 Android 开发的新手。我创建了一个带有两个视图的活动/布局。第一个是我想要 50dp 高的自定义按钮。第二个是一个列表视图,我想把它下面的屏幕的其余部分。

在此处输入图像描述

我的问题是,当我将它们相互约束时,它们ListView会被压碎为 0,并且 50dp 按钮周围的“弹簧”会展开以占据所有空间。在这里,我为列表指定了 200dp 的高度,以便您可以看到它。如果我将它设置为“匹配约束”,它会变为 0。列表中有内容。

在此处输入图像描述

有了 iOS 限制,我知道如何做到这一点。按钮周围的约束表现得像 iOS 中的“大于或等于”约束。我希望他们“平等”。你如何在 Android 中做到这一点?

这是 XML。

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

    <mycompany.PlayPauseButton
        android:id="@+id/play_pause_button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/workout_list"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/workout_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-constraintlayout

解决方案


您必须将顶部连接listView到底部,playButton而不是其他方式

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

    <mycompany.PlayPauseButton
        android:id="@+id/play_pause_button"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/workout_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_constraintTop_toBottomOf="@id/play_pause_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

干杯:)


推荐阅读