Android ConstraintSet не работает - PullRequest
0 голосов
/ 25 апреля 2018

У меня есть ConstraintLayout с именем rootLayout в моем XML.

Я бы хотел центрировать радиогруппу и текстовое представление поверх нее

Вот мой код:

                        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

1 Ответ

0 голосов
/ 25 апреля 2018

Хорошо, попробовав все вокруг, я наткнулся на этот урок: 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);
...