Programmatically connecting multiple views set to any size using ConstraintLayout

*爱你&永不变心* 提交于 2019-12-11 03:39:11

问题


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.



来源:https://stackoverflow.com/questions/41643597/programmatically-connecting-multiple-views-set-to-any-size-using-constraintlayou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!