Android ConstraintSet无法正常工作

时间:2018-04-25 05:03:25

标签: android android-constraintlayout

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

我想让无线电视组居中,文本视图位于其顶部

这是我的代码:

                        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

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,添加constraintHeight解决了这个问题

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

答案 1 :(得分:0)

好的尝试后我遇到了这个教程: https://www.techotopia.com/index.php/An_Android_ConstraintSet_Tutorial

这是工作代码:

                        RadioGroup group = new RadioGroup(this);
                        group.setOrientation(RadioGroup.VERTICAL);
                        group.setId(R.id.radioGroup);

                        TextView titleTv = new TextView(this);
                        titleTv.setText(currentQuestion.titleEn);
                        titleTv.setId(R.id.titleTv);

                        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.constrainHeight(group.getId(),
                                ConstraintSet.WRAP_CONTENT);
                        constraintSet.constrainWidth(group.getId(),
                                ConstraintSet.WRAP_CONTENT);


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

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

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

                        constraintSet.applyTo(rootLayout);