español developer dev definicion constraintlayout constraint cambiar android android-layout android-view android-constraintlayout constraint-layout

android - developer - ConstraintLayout: cambiar restricciones mediante programación



constraint layout definicion (2)

Necesito ayuda con ConstraintSet . Mi objetivo es cambiar las restricciones de la vista en el código, pero no puedo entender cómo hacerlo correctamente.

Tengo 4 TextView sy un ImageView . Necesito establecer restricciones de ImageView en uno de los TextView s.

check_answer4 = (TextView) findViewById(R.id.check_answer4); check_answer1 = (TextView) findViewById(R.id.check_answer1); check_answer2 = (TextView) findViewById(R.id.check_answer2); check_answer3 = (TextView) findViewById(R.id.check_answer3); correct_answer_icon = (ImageView) findViewById(R.id.correct_answer_icon);

Si la primera respuesta es correcta, necesito establecer restricciones de ImageView en

app:layout_constraintRight_toRightOf="@+id/check_answer1" app:layout_constraintTop_toTopOf="@+id/check_answer1"

Si la segunda respuesta es correcta, necesito establecer restricciones de ImageView en

app:layout_constraintRight_toRightOf="@+id/check_answer2" app:layout_constraintTop_toTopOf="@+id/check_answer2"

Y así.


  1. Para establecer restricciones de la vista de imagen para:

    app:layout_constraintRight_toRightOf="@+id/check_answer1" app:layout_constraintTop_toTopOf="@+id/check_answer1"

    utilizar:

    ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0); constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0); constraintSet.applyTo(constraintLayout);

  2. Para establecer restricciones de la vista de imagen para:

    app:layout_constraintRight_toRightOf="@+id/check_answer2" app:layout_constraintTop_toTopOf="@+id/check_answer2"

    utilizar:

    ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0); constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0); constraintSet.applyTo(constraintLayout);


Supongamos que queremos cambiar las restricciones durante el tiempo de ejecución, haciendo que el botón1 se alinee con el botón2 cuando se hace clic en él:

Luego, teniendo este diseño:

<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" android:id="@+id/root" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" app:layout_constraintTop_toTopOf="@+id/button3" app:layout_constraintBottom_toBottomOf="@+id/button3" app:layout_constraintStart_toEndOf="@+id/button3" android:layout_marginStart="0dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="0dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="Button 2" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.5" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="Button 3" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.223" /> </android.support.constraint.ConstraintLayout>

Podemos hacer lo siguiente:

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button1.setOnClickListener { val params = button1.layoutParams as ConstraintLayout.LayoutParams params.leftToRight = button2.id params.topToTop = button2.id params.bottomToBottom = button2.id button1.requestLayout() } }