Android Studio 约束集未为以编程方式添加的视图设置约束
Android Studio constraint set not setting constraints for programmatically added view
我有一个布局,当我关注 TexInputEditText 时需要向其添加视图。我尝试以编程方式创建一个 textview,为它设置一个 id,然后我继续使用约束集克隆我的约束布局约束。问题是,当我使用 constraintset.connect 为添加的 textview 设置约束时,无论我在那里放置什么,textview 的约束将始终为 0,0,我无法绕过它。这是代码:
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ParametersList">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/headerLayoutParameters"
android:layout_width="match_parent"
android:layout_height="69dp"
android:background="@color/dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/parametri"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintParamList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/headerLayoutParameters"
>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout23"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramAnamnesi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:inputType="none"
android:text="@string/anamnesi"
android:textColor="@color/dark"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout24"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout23"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsPlica"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/plica"
android:inputType="none"
android:textColor="@color/dark"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout25"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout24"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsMisureAntro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/misure_antropometriche"
android:inputType="none"
android:textColor="@color/dark"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout26"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout25"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsNote"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/note"
android:textColor="@color/dark"
android:inputType="none"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/addParamsLayout"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout26">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsAddParam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/add_person"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/aggiungi_personalizzato"
android:textColor="@color/dark"
android:inputType="none"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java代码:
ConstraintLayout constraintLayout;
constraintLayout=findViewById(R.id.constraintParamList);
textInputEditText5.setOnFocusChangeListener(new View.OnFocusChangeListener() { //AGGIUNTA DINAMICA LAYOUT PER NEW PARAM
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(textInputEditText5.isFocused()){
EditText editText=new EditText(getApplicationContext());
editText.setText("ADDED PARAMETER");
ConstraintSet constraintSet=new ConstraintSet();
int idCreato=8;
editText.setId(idCreato);
ConstraintLayout.LayoutParams addedParams= new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
editText.setLayoutParams(addedParams);
constraintLayout.addView(editText);
constraintSet.clone(constraintLayout);
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);
constraintSet.applyTo(constraintLayout);
//TODO FAR FUNZIONARE AGGIUNTA DINAMICA LAYOUT PARAMETRI
}else{
}
}
});
约束只能应用于 ConstraintLayout 的直接子级。您正在尝试将新视图与 TextInputEditText 连接,后者是 TextInputLayout 的子视图。而不是
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);
尝试
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.addParamsLayout, ConstraintSet.BOTTOM, 16);
看看是否有帮助。
我有一个布局,当我关注 TexInputEditText 时需要向其添加视图。我尝试以编程方式创建一个 textview,为它设置一个 id,然后我继续使用约束集克隆我的约束布局约束。问题是,当我使用 constraintset.connect 为添加的 textview 设置约束时,无论我在那里放置什么,textview 的约束将始终为 0,0,我无法绕过它。这是代码:
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ParametersList">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/headerLayoutParameters"
android:layout_width="match_parent"
android:layout_height="69dp"
android:background="@color/dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/parametri"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintParamList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/headerLayoutParameters"
>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout23"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramAnamnesi"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:inputType="none"
android:text="@string/anamnesi"
android:textColor="@color/dark"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout24"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout23"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsPlica"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/plica"
android:inputType="none"
android:textColor="@color/dark"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout25"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout24"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsMisureAntro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/misure_antropometriche"
android:inputType="none"
android:textColor="@color/dark"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout26"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout25"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsNote"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/parameters_icon"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/note"
android:textColor="@color/dark"
android:inputType="none"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/addParamsLayout"
style="@style/customInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout26">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/paramsAddParam"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/back_light"
android:drawableLeft="@drawable/add_person"
android:drawableRight="@drawable/options_icon_points"
android:drawablePadding="25dp"
android:text="@string/aggiungi_personalizzato"
android:textColor="@color/dark"
android:inputType="none"
android:textSize="14sp"
android:textStyle="bold" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java代码:
ConstraintLayout constraintLayout;
constraintLayout=findViewById(R.id.constraintParamList);
textInputEditText5.setOnFocusChangeListener(new View.OnFocusChangeListener() { //AGGIUNTA DINAMICA LAYOUT PER NEW PARAM
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(textInputEditText5.isFocused()){
EditText editText=new EditText(getApplicationContext());
editText.setText("ADDED PARAMETER");
ConstraintSet constraintSet=new ConstraintSet();
int idCreato=8;
editText.setId(idCreato);
ConstraintLayout.LayoutParams addedParams= new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
editText.setLayoutParams(addedParams);
constraintLayout.addView(editText);
constraintSet.clone(constraintLayout);
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);
constraintSet.applyTo(constraintLayout);
//TODO FAR FUNZIONARE AGGIUNTA DINAMICA LAYOUT PARAMETRI
}else{
}
}
});
约束只能应用于 ConstraintLayout 的直接子级。您正在尝试将新视图与 TextInputEditText 连接,后者是 TextInputLayout 的子视图。而不是
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.paramsAddParam, ConstraintSet.BOTTOM, 16);
尝试
constraintSet.connect(idCreato, ConstraintSet.TOP,R.id.addParamsLayout, ConstraintSet.BOTTOM, 16);
看看是否有帮助。