ConstraintLayout 에 child view 를 코드로 추가

Eungi Kim·2020년 12월 1일
0
ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.main_parent_layout);
ConstraintSet set = new ConstraintSet();

ImageView childView = new ImageView(this);
// set view id, else getId() returns -1
childView.setId(View.generateViewId());
layout.addView(childView, 0);

set.clone(parentLayout);
// connect start and end point of views, in this case top of child to top of parent.
set.connect(childView.getId(), ConstraintSet.TOP, parentLayout.getId(), ConstraintSet.TOP, 60);
// ... similarly add other constraints
set.connect(childView.getId(), ConstraintSet.BOTTOM, parentLayout.getId(), ConstraintSet.BOTTOM, 22);
set.connect(childView.getId(), ConstraintSet.START, otherView.getId(), ConstraintSet.START, 22);
set.applyTo(parentLayout);

stackoverflow

profile
Run and gun and debugun

0개의 댓글