在 ConstraintLayout 中垂直添加动态子视图

6

Android ViewGroup有以下方法来添加(子)视图:

enter image description here

我知道我们可以在xml/编程中轻松定义LinearLayout的水平/垂直方向,并添加子视图,例如:

enter image description here

类似地,对于RelativeLayout,我们可以使用ViewGroupaddViewRelativeLayout.LayoutParams的方法:

enter image description here

我也知道我们可以将LinearLayout作为ConstraintLayout中的子项并进行操作。

是否有一种可靠且推荐的方法将子视图动态添加到ConstraintLayout中?

更新:

让我给一个不太简单的例子来说明我的需求。假设您有一个View1、View2和View3。所有的V1、V2和V3都垂直对齐,一个在另一个下面,并且都是由多个TextViewImageView组成的复杂视图。根据用户操作和服务器发送的信息,我需要在原始V2和V3之间添加多个V1和V2(可以是1对V1-V2,也可以是3对V1-V2)。如果我使用ConstraintLayout,当我可以轻松使用具有垂直方向的LinearLayout时,最好是在编程时添加多个约束条件吗?

现在,就效率、性能和代码量而言,相比LinearLayout,ConstraintLayout是否最适合这个要求?


你可以使用ContraintLayout.LayoutParams。 - Rahul
为什么不在ConstraintLayout中添加一个LinearLayout,并将子视图放入其中?如果您希望它可以滚动,可以将LinearLayout放在ScrollView中。 - MrObjectOriented
@Nike15,请查看链接。这个答案可能会有所帮助。 - Jahanvi Kariya
1个回答

9
使用addView(),可以像在LinearLayout中一样将视图添加到ConstraintLayout中。不同之处在于,使用ConstraintLayout时,添加的视图必须受到限制。要通过编程方式对视图进行约束,请使用ConstraintSet

此类允许您以编程方式定义一组与ConstraintLayout一起使用的约束条件。它使您可以创建和保存约束,并将其应用于现有的ConstraintLayout。

以下是一个简短的示例:

activity_main
定义两个TextViews。将它们水平居中并位于顶部。

<android.support.constraint.ConstraintLayout 
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/topView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Top View"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/bottomView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Bottom View"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/topView" />

</android.support.constraint.ConstraintLayout>

当在该布局中添加一个新的TextView(“中间视图”)而没有设置约束时,您将看到的是这个。请注意,新视图默认位置为(0,0)。

enter image description here

假设我们希望生成的中间视图在窗口中水平居中放置在顶部视图和底部视图之间,就像这样:

enter image description here

这里是将产生此结果的代码:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Define the new TextView and add it to the ConstraintLayout. Without constraints,
    // this view will be positioned at (0,0).
    TextView middleView = new TextView(this);
    middleView.setId(View.generateViewId());
    middleView.setText("Middle View");
    middleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20.0f);
    ConstraintLayout layout = findViewById(R.id.constraintLayout);
    ConstraintLayout.LayoutParams lp =
        new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,
                                          ConstraintLayout.LayoutParams.WRAP_CONTENT);
    layout.addView(middleView, lp);

    // Move the new view into place by applying constraints.
    ConstraintSet set = new ConstraintSet();
    // Get existing constraints. This will be the base for modification.
    set.clone(layout);
    int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                                    16, getResources().getDisplayMetrics());
    // Set up the connections for the new view. Constrain its top to the bottom of the top view.
    set.connect(middleView.getId(), ConstraintSet.TOP, R.id.topView, ConstraintSet.BOTTOM, topMargin);
    // Constrain the top of the bottom view to the bottom of the new view. This will replace
    // the constraint from the bottom view to the bottom of the top view.
    set.connect(R.id.bottomView, ConstraintSet.TOP, middleView.getId(), ConstraintSet.BOTTOM, topMargin);
    // Since views must be constrained vertically and horizontally, establish the horizontal
    // constaints such that the new view is centered.
    set.centerHorizontally(middleView.getId(),ConstraintSet.PARENT_ID);
    // Finally, apply our good work to the layout.
    set.applyTo(layout);
}

我之前没有明确说明我的要求,请阅读我的更新问题。 - Nike15

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接