web-dev-qa-db-fra.com

ConstraintLayout: modifie les contraintes par programme

J'ai besoin d'aide avec ConstraintSet. Mon objectif est de changer les contraintes de vue dans le code, mais je n'arrive pas à comprendre comment le faire correctement.

J'ai 4 TextViews et un ImageView. Je dois définir les contraintes ImageView sur l'une des TextViews.

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 1ère réponse est correcte, je dois définir les contraintes de ImageView sur

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

Si la 2ème réponse est correcte, je dois définir les contraintes de ImageView sur

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

Etc.

60
Big Coach
  1. Pour définir les contraintes de la vue d'image sur:

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

    utilisation:

    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. Pour définir les contraintes de la vue d'image sur:

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

    utilisation:

    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);
    
114
vishakha yeolekar

Supposons que nous voulions changer les contraintes pendant l'exécution, en faisant en sorte que button1 soit aligné avec button2 lorsque l'utilisateur clique dessus:

enter image description here

Ensuite, ayant cette disposition:

<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>

Nous pouvons faire ce qui suit:


    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()
        }
    }
46
azizbekian