【发布时间】:2019-12-20 14:14:03
【问题描述】:
阙:看下面的xml布局文件。这里嵌套的 ConstraintLayout id 为
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 的高度。布局是空的,因为我想在运行时根据某些条件在其中膨胀不同的视图。
【问题讨论】:
-
您有两个
ConstraintLayout标签,但只有一个结束标签,要么删除其中一个,要么关闭打开的。 -
@MichaelStoddart 仔细看,第二个在声明中关闭
-
当高度为
wrap_content时,似乎ConstraintLayout 不喜欢没有孩子。一种解决方法是放置一个高度为零的 Space 视图(或将可见性设置为gone的视图)来完成这项工作。如果您愿意,这可能是一个错误报告。 -
你为什么使用嵌套的ConstraintLayout,请分享你想要实现的布局图像?
-
@Cheticamp 我现在正在使用空间视图作为解决方法......但我很好奇它为什么会发生
标签: android android-constraintlayout