将动态子视图垂直添加到 ConstraintLayout

Nik*_*e15 6 android viewgroup android-viewgroup android-constraintlayout

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

在此处输入图片说明

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

在此处输入图片说明

与 RelativeLayout 类似,我们可以使用ViewGroup'saddViewRelativeLayout.LayoutParams's 方法:

在此处输入图片说明

我也知道我们可以LinearLayout在里面ConstraintLayout像个孩子一样使用并玩它。

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

更新:让我举一个不太简单的例子,说明我想要实现的目标。

假设有一个View1,View2和View3的所有V1,V2和V3被垂直对齐的一个下面的另一个并且是由多个的复杂视图TextView秒和ImageView秒。根据用户操作和服务器发送信息,我需要在原始V2和V3之间添加多个V1和V2(可以是1对V1-V2,可以是3对V1-V2)。如果我使用的是 ConstraintLayout,当我可以轻松地使用具有垂直方向的 LinearLayout 时,以编程方式添加多个约束是否最好?

现在,就效率、性能和简洁美观的代码而言,与线性布局相比,ConstraintLayout 是否最适合此要求?

Che*_*amp 7

意见可加入ConstraintLayout使用addView(),你会用同样的方式LinearLayout。不同之处在于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>
Run Code Online (Sandbox Code Playgroud)

这是在TextView没有设置约束的情况下将新(“中间视图”)添加到此布局时您将看到的内容。请注意,新视图的默认位置为 (0,0)。

在此处输入图片说明

假设我们希望生成的中间视图放置在窗口中水平居中的顶视图和底视图之间,如下所示:

在此处输入图片说明

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

主活动.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);
}
Run Code Online (Sandbox Code Playgroud)