Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ViewStub in ConstraintLayout?

Tags:

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?

like image 416
joharei Avatar asked Nov 07 '17 17:11

joharei


People also ask

How do I add a toolbar in ConstraintLayout?

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.

Is ConstraintLayout a ViewGroup?

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

What is view stub?

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.


1 Answers

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"     /> 
like image 193
banxi1988 Avatar answered Oct 02 '22 07:10

banxi1988