Really simple question, but what's the easiest way...
# compose
c
Really simple question, but what's the easiest way to have two equally sized buttons in a row. Code in thread
t
try making 1 button
fillMaxWidth(.5f)
and the other
fillMaxWidth()
c
Instinctively... I would have thought this would work?
Copy code
Row {
Button({}, modifier = Modifier.fillMaxWidth()){ Text("1")}
Button({}, modifier = Modifier.fillMaxWidth()){ Text("2000000")}
}
but I just get button 1 showing. Then doing this didn't do the trick either
Copy code
Row {
Button({}, modifier = Modifier.fillMaxWidth(.5f)){ Text("1")}
Button({}, modifier = Modifier.fillMaxWidth(.5f)){ Text("2000000")}
}
t
make the second one
fillMaxWidth()
and the first one
fillMaxWidth(.5f)
n
Use
Modifier.weight(1.f)
on both buttons
2
c
weight wins again. I never understand when I should use one over the other. I would think that fillMaxWidth on two items in a row would lay out both items equally.
a
Think of the layout process. Children are laid out in order and when a parent is laying out child A, it doesn’t know anything about child B, which is after child A, if you don’t explicitly tell it to do so. You ask the parent to give child A max width, and the parent just give it all the space available. With
Modifier.weight()
, the parent lays out all children with a weight at last with all the knowledge of how much weight each of them want to take.
c
I just always thought that giving something a weight of 1 tells it to take up the rest of the available space and so I guess I don't understand how giving two things weight = 1 makes everything equi-width.
a
As I said the parent has all the knowledge of the weight of each of the children when laying out them.
By the way the weight here is not the same as fraction. What matters is the ratio between all the weights, not the value itself.
e
weight=1 is not some magic value
space is distributed to children proportional to their weight
scale all the weights by constant, same result
n
@Colton Idle I bring this idea from
LinearLayout
which follow the same approach 😊
c
I think at some point we considered being able to pass a percent value to
width
and
height
but often a combination of
weight
and
fillMax*(*f)
does the job
👍 1
c
@Chris Sinco [G] yeah. Sometimes I just feel like I'm just guessing on which one to use. Lol
c
Fair enough