ConstraintLayout 之 ConstraintSet 動態修改約束

使用

  1. 創建ConstraintSet 對象

    ConstraintSet set = new ConstraintSet();
    set.clone(constraintLayout);
    // 或 
    // set.clone(this, R.layout.main_activity);
    
  2. 約束元素

    set.connect(v1.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
    set.connect(v2.getId(), ConstraintSet.TOP, v1.getId(), ConstraintSet.BOTTOM, dp2px(16));
    set.applyTo(constraintLayout);
    

注意: 如果有View的隱藏顯示操作,務必顯示操作View.setVisibility(View.VISIBLE)不要在1、2步中間執行,因爲建立約束時,之前的位置約束將可能被改變,從而顯示可能失效。

官方文檔

https://developer.android.google.cn/reference/android/support/constraint/ConstraintSet?hl=zh-cn

demo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章