Android ConstraintLayout:如何在一个下方添加一个动态视图

Android ConstraintLayout: How to add a dynamic view one below another

我正在尝试在运行时在约束布局中将 TextView 一个一个地添加到另一个下方。但我总是只得到一个文本视图,其余的都隐藏在它后面。我尝试了几件事,包括链接视图,但似乎没有任何效果。

private void method(int position)
    {
        ConstraintSet set = new ConstraintSet();
        TextView textView = new TextView(getContext());
        int textViewId = 100 + position;
        //previousTextViewId = textViewId;
        textView.setId(textViewId);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, WRAP_CONTENT);
        layoutParams.rightToRight = PARENT_ID;
        layoutParams.leftToLeft = guideline_60.getId(); //Vertical GuideLine of 60%
        layoutParams.rightMargin = 8;
        textView.setLayoutParams(layoutParams);
        if (Build.VERSION.SDK_INT < 23)
        {
            textView.setTextAppearance(getContext(), R.style.textStyle);
        }
        else
        {
            textView.setTextAppearance(R.style.textStyle);
        }
        textView.setBackgroundColor(backgroundColor);
        textView.setText(categoryName);
        textView.setGravity(Gravity.CENTER);
//markerLayout is the ConstraintLayout 
        markerLayout.addView(textView, position);
        set.clone(markerLayout);
        //set.addToVerticalChain(textView.getId(),previousTextViewId,PARENT_ID);
        set.connect(textView.getId(), ConstraintSet.TOP, markerLayout.getId(), ConstraintSet.TOP, 60);
        set.applyTo(markerLayout);
    }

我期待看到这样的东西 -

你说的是所有文本视图的顶部都连接在父视图的顶部:

set.connect(textView.getId(), ConstraintSet.TOP, 
        markerLayout.getId(), ConstraintSet.TOP, 60);

你想说,一个的顶部连接到另一个的底部 - 像这样:

set.connect(textView.getId(), ConstraintSet.TOP, 
    previousTextView.getId(), ConstraintSet.BOTTOM, 60);

你的方法叫method?

我知道这是旧答案,但@Temporary 救了我一命!!我用行做了同样的事情 layout.I 没有得到动态添加行布局的完美解决方案所以这可能对其他人有帮助

这是一个简短的例子:

activity_main

<androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guideline_end"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toEndOf="@id/guideline_start"
        app:layout_constraintTop_toBottomOf="@id/cvFDToolbar">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </androidx.core.widget.NestedScrollView>

这里是动态添加的行文件。

row_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:fontFamily="@font/raleway_medium"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="@dimen/_14ssp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edtValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_5sdp"
        android:background="@color/gray"
        android:inputType="textPersonName"
        android:padding="@dimen/_5sdp"
        android:text="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/txtTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是将在 MainActivity 文件中添加行布局 dynamically.Add 的代码。

val set = ConstraintSet()

        for (i in 0..10) {

            val inflater = LayoutInflater.from(this)

            inflatedLayout = inflater.inflate(
                com.doctor24_7.R.layout.row_pre_consultation_form,
                clItem as ViewGroup,
                false
            )

            inflatedLayout.id = 100 + i
            clItem.addView(inflatedLayout, i)
            set.clone(clItem)
            if (i ==0) {
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    60
                )
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT
                )
                set.connect(
                    inflatedLayout.id,
                    ConstraintSet.TOP,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.TOP
                )
            } else {
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.LEFT
                )
                set.connect(
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT,
                    ConstraintSet.PARENT_ID,
                    ConstraintSet.RIGHT
                )
                set.connect(
                    inflatedLayout.id,
                    ConstraintSet.TOP,
                    previousid,
                    ConstraintSet.BOTTOM, resources.getDimension(R.dimen._10sdp).toInt()
                )
            }
            set.applyTo(clItem)
            previousid = inflatedLayout.id

        }