I run Code A and get Result A.
How can I make the two buttons the same width?
BTW, you know different string has different width, you can’t write a hard code such as Modifier.width(100.dp)
.
Code A
Row(
modifier =Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
Button(
modifier = Modifier,
onClick = { }
) {
Text("Short")
}
Button(
modifier = Modifier.padding(start = 10.dp),
onClick = { }
) {
Text("This is a Long")
}
}
Result A
2
Answers
You can achieve setting width of long button to short one as in the answer described here with SubcomposeLayout.
What you need to do is subcompose your layout to check which element has longer width then subcompose your items again with this width as minimum constraint.
you can change this layouWidth with constraints.maxWidth if you want it to occupy available space.
Then, instead of setting them from beginning of Composable you need to have your algorithm for laying them out at 0 y position and x position from beginning of the Composable if you want to have different spacings.
placeable.placeRelative(xPos, 0)
Usage
Result
If you are interested in both buttons splitting the full space and being equal in width, you can use weight modifier