【问题标题】:Constraint Layout without any children views with height wrap_content takes full parent height没有任何高度 wrap_content 的子视图的约束布局采用完整的父高度
【发布时间】: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 的高度。布局是空的,因为我想在运行时根据某些条件在其中膨胀不同的视图。

Here is a snapshot of the layout.

【问题讨论】:

  • 您有两个ConstraintLayout 标签,但只有一个结束标签,要么删除其中一个,要么关闭打开的。
  • @MichaelStoddart 仔细看,第二个在声明中关闭
  • 当高度为wrap_content 时,似乎ConstraintLayout 不喜欢没有孩子。一种解决方法是放置一个高度为零的 Space 视图(或将可见性设置为 gone 的视图)来完成这项工作。如果您愿意,这可能是一个错误报告。
  • 你为什么使用嵌套的ConstraintLayout,请分享你想要实现的布局图像?
  • @Cheticamp 我现在正在使用空间视图作为解决方法......但我很好奇它为什么会发生

标签: android android-constraintlayout


【解决方案1】:

这会在屏幕上创建 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>

【讨论】:

  • 嗯... whaaaaaat..??
  • 对不起我的错误答案,但其他方法是使用 androidx.constraintlayout.widget.Guideline 并为您的布局设置 layout_constraintGuide_percent
  • 指南和其他东西不起作用......我的问题不同,此外,它想知道它为什么会发生:p
猜你喜欢
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2017-08-11
相关资源
最近更新 更多