gpt4 book ai didi

Android ConstraintSet 不工作

转载 作者:太空狗 更新时间:2023-10-29 14:39:51 39 4
gpt4 key购买 nike

我的 XML 中有一个名为 rootLayout 的 ConstraintLayout

我想让 radiogroup 居中,textview 放在它的顶部

这是我的代码:

                        ConstraintLayout.LayoutParams groupParam = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);

RadioGroup group = new RadioGroup(this);
group.setOrientation(RadioGroup.VERTICAL);
group.setLayoutParams(groupParam);

TextView titleTv = new TextView(this);
titleTv.setText(currentQuestion.titleEn);
titleTv.setLayoutParams(groupParam);

for (int i = 0; i < currentQuestion.listAnswers.size(); i++) {
RadioButton btn = new RadioButton(this);
btn.setText(currentQuestion.listAnswers.get(i).titleEn);
group.addView(btn);
}

rootLayout.addView(group);
rootLayout.addView(titleTv);

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(rootLayout);

constraintSet.connect(group.getId(), ConstraintSet.TOP,
rootLayout.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(group.getId(), ConstraintSet.BOTTOM,
rootLayout.getId(), ConstraintSet.BOTTOM, 0);
constraintSet.connect(group.getId(), ConstraintSet.LEFT,
rootLayout.getId(), ConstraintSet.LEFT, 0);
constraintSet.connect(group.getId(), ConstraintSet.RIGHT,
rootLayout.getId(), ConstraintSet.RIGHT, 0);

constraintSet.connect(titleTv.getId(), ConstraintSet.BOTTOM,
group.getId(), ConstraintSet.TOP, 70);
constraintSet.connect(titleTv.getId(), ConstraintSet.LEFT,
rootLayout.getId(), ConstraintSet.LEFT, 0);
constraintSet.connect(titleTv.getId(), ConstraintSet.RIGHT,
rootLayout.getId(), ConstraintSet.RIGHT, 0);

constraintSet.applyTo(rootLayout);

但是显示不正确。我使用 ConstraintSet 的方式有什么问题吗?

编辑:这是它的样子

enter image description here

最佳答案

我有同样的问题,添加constraintHeight解决了问题

                    constraintSet.constrainHeight(titleTv.getId(),
ConstraintSet.WRAP_CONTENT);
constraintSet.constrainWidth(titleTv.getId(),
ConstraintSet.WRAP_CONTENT);

关于Android ConstraintSet 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50014359/

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