【发布时间】:2019-02-20 01:50:32
【问题描述】:
我想通过在按钮单击时添加文本字段来更新我的约束布局。到目前为止,我正在使用约束集,但我的布局没有更新。
创建附加文本字段的方法
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>
【问题讨论】:
-
很明显你试图让good question尽你所能。我在这里看到的唯一问题是你的英语有点难以理解。我会尽量猜测你想说的最好的我能说的。也许你正试图将你的方法绑定到一个按钮,所以当你点击它时,方法就会执行?
-
您提供的 XML 示例中似乎不存在您的
<Button>...所以,如果您不介意,我的回答将基于假设的<button>小部件。 . -
但是,您的问题似乎与Android: how to handle button click 重复。
-
嗨,@SebasSBM,我刚刚意识到,我应该正确地构建问题。我想以编程方式创建视图(使用约束布局),以便我可以设置它们的 ID。
标签: android android-constraintlayout androidx