使用 ConstraintLayout 子项更改 FrameLayout 大小的问题

Troubles with changing FrameLayout size with ConstraintLayout child

我有 FrameLayout,里面有 ConstraintLayout。 ConstraintLayout 具有 android:layout_height="wrap_content" 属性。 FrameLayout 具有固定大小,小于 ConstraintLayout 所需的大小。我希望当我在运行时增加 FrameLayout 高度时,ConstraintLayout 的高度也会增长,直到它的内容适合。但它不会发生。如果我切换到 FrameLayout 而不是 ConstraintLayout,或者如果我直接在 ConstraintLayout 上调用 requestLayout(),它会按预期工作。这种行为的原因可能是什么?

布局:

 <FrameLayout
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/frame_layout_bg">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/constraint_layout_bg">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:gravity="top|center_horizontal"
                    android:padding="16dp"
                    android:text="Constraint layout content"
                    android:textSize="24sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>

更改身高代码:

        val frameLayout = findViewById<ViewGroup>(res)

        val finalHeight = TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100f, resources.displayMetrics)
            .toInt()

        val layoutParams = frameLayout.layoutParams
        layoutParams.height = finalHeight

        frameLayout.layoutParams = layoutParams
        frameLayout.requestLayout()

创建example project 来演示问题

这似乎是从 2.0.2 版本开始的 ConstraintLayout 中的一个错误,我刚刚创建了一个 pull request 建议修复此问题希望它能尽快修复,您可以切换回2.0.1 避免这个问题,直到它在未来的版本中得到解决