每当我连接两个小部件时,起始元素都将不再显示。
不幸的是,我不知道此问题的原因是什么。
ConstraintLayout myLayout已在另一个布局中膨胀。

ConstraintLayout myLayout= getActivity().findViewById(R.id.myID);
myLayout.removeAllViews();

ConstraintSet constraintSet = new ConstraintSet();

CheckBox cb = new CheckBox(myLayout.getContext());
cb.setText("CHECKBOX");
cb.setId(View.generateViewId());

myLayout.addView(cb);

constraintSet.connect(cb.getId(),ConstraintSet.END,myLayout.getId(),ConstraintSet.END);
constraintSet.connect(cb.getId(),ConstraintSet.TOP,myLayout.getId(),ConstraintSet.TOP);

constraintSet.applyTo(myLayout);

最佳答案

设置约束后显示,像这样

    ConstraintLayout myLayout= findViewById(R.id.lo1);
    myLayout.removeAllViews();

    ConstraintSet constraintSet = new ConstraintSet();

    CheckBox cb = new CheckBox(myLayout.getContext());
    cb.setText("CHECKBOX");
    cb.setId(View.generateViewId());


    constraintSet.connect(cb.getId(),ConstraintSet.END,myLayout.getId(),ConstraintSet.END);
    constraintSet.connect(cb.getId(),ConstraintSet.TOP,myLayout.getId(),ConstraintSet.TOP);

    constraintSet.applyTo(myLayout);
    myLayout.addView(cb);

10-03 00:48