首页 > 解决方案 > 从最大拉伸停止空的 ConstraintLayout

问题描述

当我从我的约束布局中删除所有视图时,我希望它的高度为 0dp(就像听起来的 wrap_content 一样),但它会尽可能地伸展。

有记录的方法吗?我的意思是除了将 maxHeight 设置为0或隐藏它。

我试过app:layout_constrainedHeight="true"了,"wrap_content"但它不起作用。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constrainedHeight="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</android.support.constraint.ConstraintLayout>

标签: androidandroid-constraintlayout

解决方案


我无法找到合适的解决方案。


我想出的最简单的解决方法是添加一个额外的视图height=0并且永远不要删除它:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <FrameLayout
            android:id="@+id/somethingWithNoHeight"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

        <!--<TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum" />-->
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

我相信您应该填写一个错误以确保开发人员知道这样的问题


推荐阅读