如何以编程方式将约束布局添加到滚动视图?

How to programmatically add constraint layouts to a scrollview?

我有以下想法:

  1. 创建一个新的 Activity,其中在布局的某处包含滚动视图
  2. 创建一个 ConstraintLayout(宽度在匹配父级上),其中一个编辑字段和一个文本视图彼此相邻

=> 现在我想用按钮之类的东西向滚动视图添加任意数量的此类约束布局。

有人可以解释一下这是怎么做到的吗?这样可以吗

(在 AndroidStudio 中)

编辑: 我尝试了以下方法:

protected void addElementToScrollView() {
    ScrollView sv = getLayoutInflater()
            .inflate(R.layout.activity_goods_received_separation_on_container_level, null)
                .findViewById(R.id.scrollViewChanges);
    ConstraintLayout cl = findViewById(R.id.gc_scrollview_element);
    sv.addView(cl);
}

这是包含滚动视图的 activity 内部:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GoodsReceived_Separation_On_ContainerLevel">

    <TextView
        android:id="@+id/goods_received_num"
        android:layout_width="73dp"
        android:layout_height="27dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.601"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/goods_received_mat2" />

    <TextView
        android:id="@+id/goods_received_mat2"
        android:layout_width="73dp"
        android:layout_height="27dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.601"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="73dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_num"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="120dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_eme"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="120dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_loc"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView10" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="120dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_type"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView9" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="120dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_bme"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView7" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="73dp"
        android:layout_height="27dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_mat"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider"
        android:layout_width="368dp"
        android:layout_height="3dp"
        android:layout_marginTop="8dp"
        android:background="?android:attr/listDivider"
        android:divider="#000000"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/goods_received_num" />

    <ScrollView
        android:id="@+id/scrollViewChanges"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_marginTop="48dp"
        android:fillViewport="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView8">

    </ScrollView>

    <Button
        android:id="@+id/button_apply_gr_change"
        android:layout_width="137dp"
        android:layout_height="68dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollViewChanges" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_loc"
        app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView13"
        app:layout_constraintTop_toBottomOf="@+id/textView8"
        app:layout_constraintVertical_bias="0.2" />

    <TextView
        android:id="@+id/textView13"
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_type"
        app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
        app:layout_constraintEnd_toStartOf="@+id/textView11"
        app:layout_constraintStart_toEndOf="@+id/textView12"
        app:layout_constraintTop_toBottomOf="@+id/textView8"
        app:layout_constraintVertical_bias="0.2" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="100dp"
        android:layout_height="26dp"
        android:layout_marginBottom="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/goods_received_eme"
        app:layout_constraintBottom_toTopOf="@+id/scrollViewChanges"
        app:layout_constraintEnd_toStartOf="@+id/textView13"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView8"
        app:layout_constraintVertical_bias="0.222" />

    <View
        android:id="@+id/divider3"
        android:layout_width="368dp"
        android:layout_height="1dp"
        android:layout_marginBottom="4dp"
        android:layout_marginTop="4dp"
        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toTopOf="@+id/textView11"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView8" />

</android.support.constraint.ConstraintLayout>

这是我尝试添加到滚动视图的布局元素(任意次数,因此可以在滚动视图中同时拥有例如 10 个这样的布局元素,数量应该是动态增加)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/gc_scrollview_element"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
    android:id="@+id/spinner"
    android:layout_width="100dp"
    android:layout_height="27dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/spinner3"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Spinner
    android:id="@+id/spinner3"
    android:layout_width="100dp"
    android:layout_height="27dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/spinner2"
    app:layout_constraintStart_toEndOf="@+id/spinner"
    app:layout_constraintTop_toTopOf="parent" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="100dp"
    android:layout_height="27dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/spinner3"
    app:layout_constraintTop_toTopOf="parent" />

(我知道它包含的元素与建议的问题不同,但我认为问题没有区别。)

我的解决方案在 "sv.addView(cl);" 测试时崩溃。 (感谢您已经提出的解决方案,一旦我明白我在这里做错了什么,我会尽快测试它...)

请尝试下面的代码,我添加了两个 button,您可以将 button 替换为 textviewedittext 或任何其他控件。

建议:- 如果您以编程方式在 ConstraintLayout 中使用任何 ui 控件,请定义其 ID。

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

    ConstraintLayout constraintLayout = findViewById(R.id.constraintlayout);

    // Create btn_contact_us1
    Button btn_contact_us1 = new Button(this);
    // Generate an Id and assign it to programmatically created Button
    btn_contact_us1.setId(View.generateViewId());
    btn_contact_us1.setText("Contact Us 1");
    btn_contact_us1.setLayoutParams(new ConstraintLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    // Add programmatically created Button to ConstraintLayout
    constraintLayout.addView(btn_contact_us1);

    // Create btn_contact_us2
    Button btn_contact_us2 = new Button(this);
    // Generate an Id and assign it to programmatically created Button
    btn_contact_us2.setId(View.generateViewId());
    btn_contact_us2.setText("Contact Us 2");
    btn_contact_us2.setLayoutParams(new ConstraintLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    // Add programmatically created Button to ConstraintLayout
    constraintLayout.addView(btn_contact_us2);

    // Create ConstraintSet
    ConstraintSet constraintSet = new ConstraintSet();
    // Make sure all previous Constraints from ConstraintLayout are not lost
    constraintSet.clone(constraintLayout);

    // Create Rule that states that the START of btn_contact_us1 will be positioned at the END of btn_contact_us2
    constraintSet.connect(btn_contact_us2.getId(), ConstraintSet.START, btn_contact_us1.getId(), ConstraintSet.END);
    constraintSet.applyTo(constraintLayout);    
}

布局代码如下

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
    android:id="@+id/constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</ScrollView>

输出:- check link image

<?xml version="1.0" encoding="utf-8"?>
 <ScrollView android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/B1"/>
</android.support.constraint.ConstraintLayout> </ScrollView>