【问题标题】:How to use ViewStub in ConstraintLayout?如何在 ConstraintLayout 中使用 ViewStub?
【发布时间】:2017-11-07 17:50:58
【问题描述】:

似乎当在ConstraintLayout 中膨胀ViewStub 时,生成的视图已经失去了所有的约束。我想我们可以使用ConstraintSet 定义膨胀视图的约束,但这违背了ViewStub 的目的。

有什么好办法吗?

【问题讨论】:

    标签: android android-constraintlayout viewstub


    【解决方案1】:

    有一个简单的解决方案:

    在您的 ViewStub 中让 inflatedId 属性与 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"
        />
    

    【讨论】:

    • 同时工作和零意义。因为事情应该在android上。
    • @Ghedeon idViewSub 的句柄,inflatedId 是取代它的结果布局的句柄。
    猜你喜欢
    • 2021-09-03
    • 2021-06-16
    • 2015-07-25
    • 2017-02-04
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    相关资源
    最近更新 更多