androidx / constraintlayout

ConstraintLayout is an Android layout component which allows you to position and size widgets in a flexible way
Apache License 2.0
1.07k stars 176 forks source link

[Compose] How to achieve the same "bias" effect as xml #744

Closed Sinyuk7 closed 1 year ago

Sinyuk7 commented 1 year ago

Hi, in Compose, if I arrange 3 Boxes vertically, and the middle Box depends on the upper and lower 2 Boxes:

top.linkTo(box1.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
bottom.linkTo(box3.top)

Then if I want to make Box 2 as close as possible to Box 1 or Box 3, this is easy to do in Xml by setting its bias:

<Box app:layout_constraintVertical_bias="0..1" />

But in Compose, the only thing I can find related to the keyword "bias" is ChainStyle.Pack(). Unfortunately Chain overrides Box's original constraints (Although I know that is what is written in the Compose code)

Screen Shot 2022-10-05 at 19 09 47
  1. origin
  2. createVerticalChain(box2)
  3. createVerticalChain(box1, box2, box3)

Of course, in alternative way, I can put Box2 in a "FrameLayout" and using its alignment to achieve “bias”. And this FrameLayout replaces the original Box2's position in the ConstraintLayout.

Screen Shot 2022-10-05 at 19 22 30

Well this approach is obviously not elegant, I'm wondering if I'm using Chain incorrectly, or if there are other ways to achieve a similar effect

oscar-ad commented 1 year ago

At the moment you'll have to use linkTo(start, end, ..., bias). See ConstrainScope.linkTo.

But since it's not always the most convenient way, we'll make all those parameters available at the property level.

Sinyuk7 commented 1 year ago

Opps...I almost lost a linkTo method here in [ConstrainScope]. I thought only a Anchorable reference has linkTo method, which has limited parameters:

  fun linkTo(
        anchor: ConstraintLayoutBaseScope.HorizontalAnchor,
        margin: Dp = 0.dp,
        goneMargin: Dp = 0.dp
 )

thanks for reply!