ConstraintLayout:如何以编程方式添加多个视图?

9

我想在ConstraintLayout中添加2个按钮。 我当前的代码如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
    ConstraintSet set = new ConstraintSet();
    set.clone(layout);

    //Button 1: 
    Button button = new Button(this);
    button.setText("Hello");
    layout.addView(button);

    set.connect(button.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 0);
    set.connect(button.getId(), ConstraintSet.RIGHT, layout.getId(), ConstraintSet.RIGHT, 0);
    set.connect(button.getId(), ConstraintSet.BOTTOM, layout.getId(), ConstraintSet.BOTTOM, 0);
    set.constrainWidth(button.getId(), ConstraintSet.MATCH_CONSTRAINT);
    set.constrainHeight(button.getId(), 200);
    set.applyTo(layout);


    //Button 2:     
    Button newButton = new Button(this);
    newButton.setText("Yeeey");
    layout.addView(newButton);

    set.connect(newButton.getId(), ConstraintSet.BOTTOM, button.getId(), ConstraintSet.TOP, 0);
    set.connect(newButton.getId(), ConstraintSet.LEFT, button.getId(), ConstraintSet.LEFT, 0);
    set.connect(newButton.getId(), ConstraintSet.RIGHT, button.getId(), ConstraintSet.RIGHT, 0);
    set.constrainHeight(newButton.getId(), 200);
    set.applyTo(layout);

}

但是我只看到了一个可见的按钮(另一个可能隐藏在这个按钮后面),而且它在屏幕左上角。应该有两个按钮,位于屏幕底部,彼此链接。

我做错了什么?

输入图像描述

期望结果:

输入图像描述

1个回答

26

以下是您想要实现的代码

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
        ConstraintSet set = new ConstraintSet();
        set.clone(layout);

        //Button 1:
        Button button = new Button(this);
        button.setText("Hello");
        button.setId(100);           // <-- Important
        layout.addView(button);
        set.connect(button.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        set.connect(button.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
        set.connect(button.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
        set.constrainHeight(button.getId(), 200);
        set.applyTo(layout);


        //Button 2:
        Button newButton = new Button(this);
        newButton.setText("Yeeey");
        layout.addView(newButton);
        set.connect(newButton.getId(), ConstraintSet.BOTTOM, button.getId(), ConstraintSet.TOP, 0);
        set.connect(newButton.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
        set.connect(newButton.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
        set.constrainHeight(newButton.getId(), 200);
        set.applyTo(layout);


    }

重要提示:
如果未显式设置id,所有元素都将获得相同的id(-1)。


太好了。这是可行的代码。我可以看到你添加了“button.setID(100);”并将“layout.getId()”更改为“ConstraintSet.PARENT_ID”。你能解释一下为什么这样做使它工作吗? - Einar
我们想将按钮设置在父布局的底部,因此使用“ConstraintSet.PARENT_ID”更为合适。在将newButton设置到button的顶部时,我之前使用了代码,但它并没有给我完美的输出,所以我必须使用Java代码来设置id。 - Darshan Soni
6
我明白了,谢谢。对于其他人阅读这些评论的人:我打印了每个按钮的.getId()内容。如果未使用.setId(),则默认将ID设置为-1。这意味着所有的按钮都有一个ID为-1。在这种情况下,你的set.connect()将无法区分一个按钮和另一个按钮。 - Einar
1
@Einar,你的解释真的帮了我很多。setId是解决我的问题的关键。 - Robert J. Clegg
2
Einar的评论真的很有帮助,因此已添加到答案中。 - Jithin Pavithran
6
你应该使用View.generateViewId()方法代替使用整型常量作为参数。 - user8389458

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接