android Studio:以编程方式将多个元素添加到 ConstraintLayout

androidStudio : add multiple elements to ConstraintLayout programmatically

我已经在 android 工作室建立了一个新项目。 兼容性 Android 9 Java.

我想在 for 循环中将多个元素添加到视图/布局。

这是否适用于 ConstraintLayout,还是我必须将整个项目切换到 LinearLayout?

这不起作用,TextView 项目未显示:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createTextViews();
    }



    private void createTextViews() {
        for (int i = 0; i < 10; i++) {


        ConstraintLayout constraintLayout = findViewById(R.id.MainActivity);
            //constraintLayout.setId(ConstraintLayout.generateViewId());
            ConstraintSet set = new ConstraintSet();

            TextView tv = new TextView(this);
            tv.setText("sdfs");
            tv.setTextSize(45);
            tv.setId(TextView.generateViewId());

            ConstraintLayout.LayoutParams cltv = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
            tv.setLayoutParams(cltv);

            constraintLayout.addView(tv,0);

            set.clone(constraintLayout);
            set.connect(tv.getId(), ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.TOP, 60);
            set.applyTo(constraintLayout);
  }
}

如果您想以编程方式将视图添加到约束布局,您还必须添加约束以使其正确显示。例如,这里是设置文本视图垂直堆栈的方法。

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

    ConstraintLayout cl = findViewById(R.id.parent_constraint);
    int lastId = -1;

    // first add the views
    for (int i = 0; i < 10; i++) {
        TextView tv = new TextView(this);
        tv.setText("Entry " + i);
        tv.setTextSize(20);
        int id = TextView.generateViewId();
        tv.setId(id);

        ConstraintLayout.LayoutParams cltv = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        
        // you can add constraints here, or save off a list of 
        // the view IDs and set up constraints in a separate step
        cltv.startToStart = ConstraintSet.PARENT_ID;
        cltv.endToEnd = ConstraintSet.PARENT_ID;

        // for the first view, constrain it to the top,
        // subsequent views get stacked below the previous one
        if( i == 0 ) {
            cltv.topToTop = ConstraintSet.PARENT_ID;
        }
        else {
            cltv.topToBottom = lastId;
        }
        tv.setLayoutParams(cltv);

        cl.addView(tv);
        lastId = id;
    }
}

如果您想先创建,保存视图 ID 列表,然后是布局(对于更复杂的布局),您可以使用这样的 ConstraintSet

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

    ConstraintLayout cl = findViewById(R.id.parent_constraint);
    ArrayList<Integer> ids = new ArrayList<>();

    // first create and add the views to the constraint layout
    for (int i = 0; i < 10; i++) {
        TextView tv = new TextView(this);
        tv.setText("Entry " + i);
        tv.setTextSize(20);
        int id = TextView.generateViewId();
        tv.setId(id);
        ids.add(id);

        ConstraintLayout.LayoutParams cltv = new ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);

        tv.setLayoutParams(cltv);

        cl.addView(tv);
    }

    // Then set constraints
    ConstraintSet set = new ConstraintSet();
    set.clone(cl);

    for (int i = 0; i < 10; i++) {

        // start_toStartOf=parent
        set.connect(ids.get(i), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);

        // end_toEndOf=parent
        set.connect(ids.get(i), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);

        if( i == 0 ) {
            //top_toTopOf=parent
            set.connect(ids.get(i), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
        }
        else {
            //top_toBottomOf=view[n-1]
            set.connect(ids.get(i), ConstraintSet.TOP, ids.get(i-1), ConstraintSet.BOTTOM, 0);
        }
    }

    set.applyTo(cl);
}