https://kotlinlang.org logo
v

Vsevolod Ganin

01/16/2021, 1:45 PM
I wonder why the
matchParentSize
modifier is available only in
BoxScope
? It could be useful in any layout scope I think, as well as
matchParentWidth
and
matchParentHeight
2
t

Timo Drick

01/16/2021, 1:47 PM
Maybe you want to use the fillMaxSize()/fillMaxHeight/Width() modifier?
v

Vsevolod Ganin

01/16/2021, 1:52 PM
Unfortunately it’s not the same. Here is an example
Copy code
Column {
    Text(...)
    Canvas(modifier = Modifier.fillMaxWidth().height(1.dp)) {
        drawLine(color, Offset(0f, 0f), Offset(size.width, 0f))
    }
    Text(...)
}
And I get
While I really don’t want to take
Canvas
into account while measuring, I just want to set it to be in size as the rest of the content dictates
t

Timo Drick

01/16/2021, 1:57 PM
And when you wrap the Canvas into a Box that have .matchParentSize modifier it works?
Oh sorry. No that will not work
Yea i think you are right. It would be nice to have this modifier in every layout.
r

romainguy

01/16/2021, 7:52 PM
Constraints like match parent available in all layouts unfortunately sometimes leads to ambiguous setups
An obvious example with the View system is a linear layout set to wrap content and all its children set to match parent
a

Adam Powell

01/16/2021, 8:02 PM
it's not available in every layout because it requires some special handling by the parent, and we don't require that every custom layout perform that special handling.
matchParentSize
in Box is an instruction to measure that element last, with whatever specific size the Box has already determined for itself, potentially from measuring its other child elements first.
you can get a similar effect along a single dimension by sizing the container using intrinsic measurements; see this example: https://developer.android.com/codelabs/jetpack-compose-layouts#9
👍 5
t

Timo Drick

01/16/2021, 8:20 PM
Thanks @Adam Powell Works:
Copy code
Column(Modifier.preferredWidth(IntrinsicSize.Min)) {
        Text(text = "4")
        Divider(modifier = Modifier.fillMaxWidth().preferredHeight(1.dp),color = Color.Black)
        Text(text = "2")
    }
👍 4
👍🏼 1