ConstraintLayout:如何以编程方式添加多个视图? [英] ConstraintLayout: How to add several views programmatically?
本文介绍了ConstraintLayout:如何以编程方式添加多个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ConstraintLayout中添加2个按钮。我当前的代码如下:
I want to add 2 buttons to a ConstraintLayout. My current code is as following:
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个按钮相互链接。
But I only get 1 visible button (the other is probably hidden behind this one), and it's in the top left corner of the screen. There's supposed to be 2 buttons, at the bottom of the screen, linked to each other.
我在这里做错了什么?
预期结果:
推荐答案
以下是您要实现的工作代码
Here is the working code of what you want to achieve
@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)。
Important:
If id
is not set explicitly, all the elements will get the same id(-1).
这篇关于ConstraintLayout:如何以编程方式添加多个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文