使用 ConstraintLayout 以编程方式连接设置为任意大小的多个视图

Programmatically connecting multiple views set to any size using ConstraintLayout

我正在使用 ConstraintLayout beta4 并尝试以编程方式在屏幕上创建水平链。

我 运行 遇到的问题是当我在屏幕上将多个视图以链的形式连接在一起时 (ex: Left side of layout <-> leftButton <-> rightButton <-> Right side of layout) 然后什么都没有显示。

这是不起作用的代码:

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

}
}

这里是上面代码的截图:programmatically created.

下面是一些xml描述相同的东西并且工作正常,供参考:

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

这是 xml 的屏幕截图:manually created

您的操作存在一些问题。首先,您将同一个 LayoutParams 实例分配给两个对象——这是行不通的。您需要一个单独的实例:

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

其次,你连接错了:

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

应该是:

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

否则它不会创建链。

添加视图后克隆布局可能会更清晰。