代码之家  ›  专栏  ›  技术社区  ›  Nike15 Mark Byers

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

  •  2
  • Nike15 Mark Byers  · 技术社区  · 6 年前

    安卓 ViewGroup 使用以下方法添加(子)视图:

    enter image description here

    我知道我们可以很容易地定义水平/垂直方向,以xml/编程的方式进行线性布局,并添加子视图,例如。

    enter image description here

    与RelativeLayout类似,我们可以使用 视图组 是的 addView RelativeLayout.LayoutParams 作者的方法:

    enter image description here

    我也知道我们可以使用 LinearLayout 作为一个孩子 ConstraintLayout 玩它。

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

    使现代化 : 让我举一个不那么简单的例子来说明我想要实现的目标。

    假设你有一个 View 1. 看法 2和 看法 3.所有V1、V2和V3都是垂直对齐的,一个接一个,是由多个视图组成的复杂视图 TextView s和 ImageView s、 根据用户操作和服务器发送的信息,我需要在原始V2和V3之间添加多个V1和V2(可以是1对V1-V2,也可以是3对V1-V2)。如果我使用的是ConstraintLayout,当我可以轻松地使用带有垂直方向的LinearLayout时,我是否最好以编程方式添加多个约束?

    现在,在效率、性能和更少漂亮的代码方面,与线性布局相比,ConstraintLayout最适合这个需求吗?

    1 回复  |  直到 6 年前
        1
  •  8
  •   Cheticamp    6 年前

    视图可以添加到 ConstraintLayout 使用 addView() 就像你和我一样 LinearLayout .不同之处在于 约束青年 添加的视图必须受到约束。要以编程方式约束视图,请使用 ConstraintSet .

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

    下面是一个简单的例子:

    主要活动
    定义两个 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

    下面是生成此结果的代码:

    主要活动。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);
    }