ConstraintLayout和以编程方式更改约束

时间:2019-04-06 08:35:27

标签: android android-layout android-view android-constraintlayout

我正在尝试通过编程方式更改约束,如下所示:

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); 

布局正确!但是我不明白为什么。
thisConstraintLayout,因为我要扩展ConstraintLayout

0 个答案:

没有答案