没有任何子级的约束布局 具有高度wrap_content的视图采用完整的父级高度



Que:查看下面的xml布局文件。这里是嵌套的约束布局 IDclLoginStub高度设置为wrap_content但它 仍然占据了屏幕的整个高度。为什么会发生这种情况以及如何解决?

<?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"
android:background="#f4f0f0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clLoginStub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#d50000"
android:paddingBottom="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/xviewGuideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="@id/clLoginStub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvLoginMethods"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#6fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/xviewGuideline"
tools:itemCount="3"
tools:listitem="@layout/sign_in_button" />
</androidx.constraintlayout.widget.ConstraintLayout>

我以为这将是一个IDE错误,但它也会在运行应用程序时发生。

当我放置一个子元素时,布局工作正常。当它为空时,就会出现问题。相反,当它为空时,它应该具有 0dp 高度。布局是空的,因为我想在运行时根据某些条件膨胀其中的不同视图。

下面是布局的快照。

这会在屏幕上创建 3 个相同高度的布局

<?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"
android:background="#f4f0f0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clLoginStub"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#d50000"
android:paddingBottom="80dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/xviewGuideline"/>
<View
android:id="@+id/xviewGuideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="80dp"
app:layout_constraintTop_toBottomOf="@id/clLoginStub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/rvLoginMethods"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvLoginMethods"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#6fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/xviewGuideline"
tools:itemCount="3" />
</androidx.constraintlayout.widget.ConstraintLayout>
<View
android:id="@+id/xviewGuideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="80dp"
app:layout_constraintBottom_toBottomOf="@id/clLoginStub"
app:layout_constraintTop_toBottomOf="@id/clLoginStub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

就我而言,我输入了"app:layout_constraintTop_toBottomOf",它解决了。

最新更新