Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have FrameLayout with ConstraintLayout inside it. ConstraintLayout has android:layout_height="wrap_content" attribute. FrameLayout has fixed size which is smaller than desired by ConstraintLayout. I expect that when I increase FrameLayout height at runtime, height of ConstraintLayout will also grow until it's content fit. But it does not happen. It works as expected if I switch to FrameLayout instead of ConstraintLayout or if I call requestLayout() directly on ConstraintLayout. What could be the reason of such behavior?

Layout:

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

Change height code:

        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()

Created example project to demonstrate the issue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
426 views
Welcome To Ask or Share your Answers For Others

1 Answer

This seems to be a bug in ConstraintLayout that started with the version 2.0.2, I just created a pull request suggesting a fix for this issue hope it will be fixed soon, you can just switch back to 2.0.1 to avoid this issue till it gets resolved in future versions


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...