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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…