gpt4 book ai didi

java - 约束布局 : How to add several views programmatically?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:11 26 4
gpt4 key购买 nike

我想向 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 个可见按钮(另一个可能隐藏在这个按钮后面),它位于屏幕的左上角。屏幕底部应该有 2 个按钮,它们相互链接。

我在这里做错了什么?

enter image description here

期望的结果:

enter image description here

最佳答案

这是你想要实现的工作代码

  @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)。

关于java - 约束布局 : How to add several views programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41666566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com