Removing/Adding constraint programmatically in ConstraintLayout

后端 未结 3 888
囚心锁ツ
囚心锁ツ 2020-12-14 05:47

I want to remove and add constraint programmatically based on some condition. Here are the screenshots:

and I want to remove it like this but in code:

3条回答
  •  佛祖请我去吃肉
    2020-12-14 06:12

    I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet.

        ConstraintSet set = new ConstraintSet();
        ConstraintLayout layout;
    
        layout = (ConstraintLayout) findViewById(R.id.layout);
        set.clone(layout);
        // The following breaks the connection.
        set.clear(R.id.bottomText, ConstraintSet.TOP);
        // Comment out line above and uncomment line below to make the connection.
        // set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
        set.applyTo(layout);
    

提交回复
热议问题