我想通过在按钮单击时添加文本字段来更新我的约束布局。到目前为止,我正在使用约束集,但我的布局没有更新。
创建附加文本字段的方法
private void createUtilForm(){
titleInputLayout = new TextInputLayout(this);
int titleId = ViewCompat.generateViewId();
Log.d("AddEmployee", "createUtilForm: titleId"+ titleId);
titleInputLayout.setId(titleId);
ConstraintLayout.LayoutParams clpTitle = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
childConstraintLayout.addView(titleInputLayout, clpTitle);
ConstraintSet utilFordSet = new ConstraintSet();
utilFordSet.clone(childConstraintLayout);
utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
utilFordSet.connect(titleInputLayout.getId(), ConstraintSet.TOP, allowanceHeaderTV.getId(), ConstraintSet.BOTTOM);
utilFordSet.connect(deductionHeaderTV.getId(), ConstraintSet.TOP, titleInputLayout.getId(), ConstraintSet.BOTTOM);
utilFordSet.applyTo(childConstraintLayout);
}
XML 布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddEmployeeActivity">
<ScrollView
android:id="@+id/child_scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_employee_toolbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/child_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/allowance_header_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/allowance_header_hint"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/allowance_add_ib"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent" />
<ImageView
android:id="@+id/allowance_add_ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="@+id/allowance_header_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/allowance_header_tv"
app:srcCompat="@drawable/ic_add" />
<TextView
android:id="@+id/deduction_header_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="@string/deduction_header_hint"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/deduction_add_ib"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/allowance_header_tv" />
<ImageView
android:id="@+id/deduction_add_ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:contentDescription="@string/img_desc_label"
app:layout_constraintBottom_toBottomOf="@+id/deduction_header_tv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/deduction_header_tv"
app:srcCompat="@drawable/ic_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>