android - 如何让 ConstraintLayout 中的 View 填充可用高度?

标签 android android-constraintlayout

我是 Android 开发新手。我创建了一个有两个 View 的 Activity/布局。第一个是我想要 50dp 高的自定义按钮。第二个是 ListView ,我想在它下面显示屏幕的其余部分。

enter image description here

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

enter image description here

有了 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>

最佳答案

您必须将 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>

干杯:)

关于android - 如何让 ConstraintLayout 中的 View 填充可用高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57332059/

相关文章:

android - 如何使用 jetpack compose 创建圆形轮廓按钮

安卓工作室 : Missing Strip Tool

android - 如何使用 ConstraintLayout 设置 maxHeight over DimensionRatio?

java - 使用约束布局针对不同的屏幕尺寸进行设计

android - 如何使用 ConstraintLayout 布局一排按钮

android - 如何在单个 View 上创建多个转换

android - 将大图设置为 SurfaceView 的背景,使其适合 Android 中的显示高度

java - 将 List<Observable<X[]> 扁平化为 Observable<X[]>

android - 在没有eclipse的情况下将android项目编译为apk

Android - 如何制作可滚动的约束布局?