android - 如何使 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>
解决方案
您必须将顶部连接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>
干杯:)
推荐阅读
- docker - kubernetes - GCP - 无法连接到 https://collector.newrelic.com
- c++ - 读取帧缓冲区对象的深度缓冲区
- angular - 基于另一个数据的角度离子解析/显示数据
- azure - Flutter Azure 人脸 API
- android - Android 传感器数据到 Cloud pubsub
- html - bootstrap-4 并排卡宽度响应问题
- amazon-cloudformation - CFT 到 CDK 迁移 - 如何从 CDK 代码引用 CFT 创建的对象
- java - IntelliJ 的 Java maven 代码中的 scala 和 spark 版本不匹配错误
- keras - 当我的批量大小为 1 时,为什么在分配形状为 [24,64,1024,1024] 的张量时出现 ResourceExhaustedError:OOM?
- php - 即使添加新的 shell_exec 来停止另一个,shell_exec 也不会停止