我正在尝试通过编程方式更改约束,如下所示:
final ConstraintSet set = new ConstraintSet();
set.clone(this);
if(text == null) {
view.setVisibility(GONE);
set.clear(div.getId(), ConstraintSet.TOP);
set.connect(div.getId(), ConstraintSet.TOP, otherView.getId(), ConstraintSet.BOTTOM);
}
set.applyTo(this);
但是该规则不适用。 div
到达布局的顶部,就像没有顶部约束一样。
我究竟做错了什么?
更新
如果我更改以下内容:
set.clone(this);
...
set.apply(this);
至:
View root = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, this);
ConstraintLayout layout = root.findViewById(R.id.constraint_layout);
....
set.clone(layout);
....
set.apply(layout);
布局正确!但是我不明白为什么。
this
是ConstraintLayout
,因为我要扩展ConstraintLayout
。