Align ConstraintLayout item to be at the end of two items

前端 未结 2 1342
萌比男神i
萌比男神i 2021-02-13 06:02

I have a ConstraintLayout with two views A and B that are stacked vertically. I have a third view C which needs to be to the end of both A and B horizontally. At any given point

2条回答
  •  Happy的楠姐
    2021-02-13 06:37

    I tried this out in my IDE and i came up with some code to do it dynamically at runtime

    TextView viewa = (TextView) findViewById(R.id.view_a);
    TextView viewb = (TextView) findViewById(R.id.view_b);
    
    ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.constraintLayout);
    ConstraintSet cs = new ConstraintSet();
    cs.clone(cl);
    cs.connect(viewb.getWidth() > viewa.getWidth() ? R.id.view_b : R.id.view_a, ConstraintSet.RIGHT, R.id.view_c, ConstraintSet.LEFT);
    cs.applyTo(cl);
    

    This ends up messing it in a slight way as it pushes the view which is wider out of the screen on the left. Maybe you could figure that out as i am unable to at this point.

提交回复
热议问题