使用ConstraintLayout以编程方式将多个视图集设置为任意大小

时间:2017-01-13 21:19:09

标签: android android-layout programmatically-created android-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

的屏幕截图

1 个答案:

答案 0 :(得分:1)

你正在做的事情有几个问题。首先,您要为两个对象分配相同的LayoutParams实例 - 这些实例不会起作用。您需要一个单独的实例:

dat <- read.table(textConnection(txt), skip = skip)
str(dat)
# 'data.frame': 4 obs. of  2 variables:
#  $ V1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4
#  $ V2: int  2 4 6 8

其次,你的连接错误了:

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

应该是:

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

否则它不会创建链条。

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