【问题标题】:Android ConstraintSet not workingAndroid 约束集不起作用
【发布时间】:2021-03-20 00:39:12
【问题描述】:

我的 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 的方式有什么问题吗?

编辑:这是它的样子

【问题讨论】:

  • 只是一个旁注,你为什么不使用布局编辑器?通常您可以立即看到问题所在
  • 这是个好主意,但我必须动态添加这些布局(因此必须通过代码添加它们,而不是 XML 布局)。我想我还是会试一试。
  • 你可以使用布局充气器来添加动态视图,然后你可以使用 xml 编辑器,我认为它更容易

标签: android android-constraintlayout


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    好吧,在尝试了一些事情之后,我遇到了这个教程: 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);
    

    【讨论】:

    • 仅仅粘贴带有链接的代码是不够的
    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2015-03-13
    • 2017-08-24
    • 2014-11-20
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多