Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

android - Troubles with changing FrameLayout size with ConstraintLayout child

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


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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...