android - 从最大拉伸(stretch)中停止空 ConstraintLayout

标签 android android-constraintlayout

当我从 Constraint 布局中删除所有 View 时,我希望它具有 0dp 高度(听起来像 wrap_content)但它会尽可能地拉伸(stretch)。

有记录的方法吗?我的意思是除了将 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>

最佳答案

我找不到合适的解决方案。


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

<?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>

我认为您应该填写一个错误以确保开发人员知道此类问题

关于android - 从最大拉伸(stretch)中停止空 ConstraintLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363229/

相关文章:

android - 在不同的 Activity 之间共享线程

java - 两次之间的差异始终返回 2 或更大

android - 搜索推特时出现 twitter4j 错误

android - 约束布局按钮文本居中对齐

android - ConstraintLayout 问题(最大宽度、尺寸比和链样式)

android - 将android连接到本地django

android - PagerTabStrip 和 PagerTitleStrip 的区别?

android - Snackbar 在约束布局中与 float 操作栏重叠

android - NestedScrollView 具有约束布局不滚动

android - 未知属性 layout_width