Que:查看下面的xml布局文件。这里是嵌套的ConstraintLayoutID为
clLoginStub
的高度已设置为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高度。布局为空,因为我想根据某些条件在运行时在其中添加不同的视图。
尝试使父约束布局到wrap_content
<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="wrap_content"
android:background="#f4f0f0">