如何在 ConstraintLayout 中使用 ViewStub? [英] How to use ViewStub in ConstraintLayout?

查看:44
本文介绍了如何在 ConstraintLayout 中使用 ViewStub?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在对 ConstraintLayout 中的 ViewStub 进行膨胀时,结果视图失去了所有约束.我想我们可以使用 ConstraintSet 定义膨胀视图的约束,但这违背了 ViewStub 的目的.

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.

有什么好的办法吗?

推荐答案

有一个简单的解决方案:

There's a simple solution:

在您的 ViewStub 中让 inflatedId 属性与 id

in your ViewStub let the inflatedId attribute as the same as id

像这样:

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

这篇关于如何在 ConstraintLayout 中使用 ViewStub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆