It seems that when inflating a ViewStub
in a ConstraintLayout
, the resulting view has lost all its constraints. I guess we can define the constraints for the inflated view using ConstraintSet
, but that kind of defeats the purpose of ViewStub
.
Is there a good way to do this?
Drag and place it as a child of ConstraintLayout. To make its appearance similar to ActionBar, add the AppBarLayout in the activity_main. xml file in such a manner that the Toolbar becomes its child. Add the top, left, and right constraint of the AppBarLayout.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).
android.view.ViewStub. A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views.
There's a simple solution:
in your ViewStub let the inflatedId
attribute as the same as id
like this:
<ViewStub android:id="@+id/pocket_view" android:inflatedId="@+id/pocket_view" android:layout="@layout/game_pocket_view" android:layout_width="@dimen/game_pocket_max_width" android:layout_height="@dimen/game_pocket_max_height" app:layout_constraintLeft_toLeftOf="@id/avatar_view" app:layout_constraintRight_toRightOf="@id/avatar_view" app:layout_constraintTop_toTopOf="@id/avatar_view" app:layout_constraintBottom_toBottomOf="@id/avatar_view" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With