-->

Programmatically connecting multiple views set to

2019-07-09 08:33发布

问题:

I'm using ConstraintLayout beta4 and trying to create a horizontal chain across the screen programmatically.

The problem I'm running into is when I connect multiple views together in a chain across the screen (ex: Left side of layout <-> leftButton <-> rightButton <-> Right side of layout) then nothing shows up at all.

Here is the code that does not work:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ConstraintLayout layout = (ConstraintLayout) View.inflate(this, R.layout.activity_main, null);
    setContentView(layout);

    ConstraintSet set = new ConstraintSet();
    set.clone(layout);

    ConstraintLayout.LayoutParams anySizeParams = new ConstraintLayout.LayoutParams(0,0);

    ImageButton leftButton = new ImageButton(this);
    leftButton.setId(View.generateViewId());
    leftButton.setLayoutParams(anySizeParams);
    layout.addView(leftButton);

    ImageButton rightButton = new ImageButton(this);
    rightButton.setId(View.generateViewId());
    rightButton.setLayoutParams(anySizeParams);
    layout.addView(rightButton);

    set.connect(leftButton.getId(),ConstraintSet.LEFT,layout.getId(),ConstraintSet.LEFT,0);
    set.connect(leftButton.getId(),ConstraintSet.RIGHT,rightButton.getId(),ConstraintSet.LEFT,0);
    set.connect(leftButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
    set.connect(leftButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);

    set.connect(rightButton.getId(),ConstraintSet.LEFT,rightButton.getId(),ConstraintSet.RIGHT,0);
    set.connect(rightButton.getId(),ConstraintSet.RIGHT,layout.getId(),ConstraintSet.RIGHT,0);
    set.connect(rightButton.getId(),ConstraintSet.TOP,layout.getId(),ConstraintSet.TOP,0);
    set.connect(rightButton.getId(),ConstraintSet.BOTTOM,layout.getId(),ConstraintSet.BOTTOM,0);

    set.applyTo(layout);

}
}

And here is a screenshot of the above code: programmatically created.

Below is some xml that describes the same thing and works fine, for reference:

<ImageButton
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/right_button"
    app:layout_constraintLeft_toRightOf="@+id/left_button"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />

<ImageButton
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/left_button"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/right_button"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

Here is a screenshot of the xml: manually created

回答1:

There's a couple of problems with what you are doing. First, you are assigning the same LayoutParams instance to both objects -- that won't work. You need a separate instance:

ConstraintLayout.LayoutParams anySizeParams2 = new ConstraintLayout.LayoutParams(0, 0);
rightButton.setLayoutParams(anySizeParams2);

Second, you got the connections wrong:

set.connect(rightButton.getId(),ConstraintSet.LEFT,
            rightButton.getId(),ConstraintSet.RIGHT,0);

should be:

set.connect(rightButton.getId(),ConstraintSet.LEFT,
            leftButton.getId(),ConstraintSet.RIGHT,0);

As otherwise it's not going to create a chain.

It probably would be cleaner to clone the layout after you added the views too.