Dmitry Akishin
10/26/2022, 10:55 AMStylianos Gakis
10/26/2022, 11:05 AMDmitry Akishin
10/26/2022, 11:09 AMStylianos Gakis
10/26/2022, 11:11 AMDmitry Akishin
10/26/2022, 11:47 AMFilip Wiesner
10/26/2022, 11:48 AMStylianos Gakis
10/26/2022, 11:49 AMDmitry Akishin
10/26/2022, 12:16 PMFilip Wiesner
10/26/2022, 12:23 PMText has to occupy all the available space between Red and Blueand
SomeText has to be centered in its parentare contradictory statements. If there really is a guarantee that red and blue will leave enough space for SomeText to be centered, then what is the problem? Just put everything in Box and align red to start, blue to end and text to center. If there is no such guarantee then create a custom layout with a behavior you want. It shouldn't be too difficult.
Stylianos Gakis
10/26/2022, 12:30 PM@Composable
fun CustomLayout(
red: @Composable RowScope.() -> Unit,
centeredText: @Composable (textAlignment: TextAlign) -> Unit,
blue: @Composable RowScope.() -> Unit,
maxWidthPercentageCenteredTextShouldTake: Float = 0.5f,
) {
Layout(
content = {
Row(Modifier.layoutId("red").border(1.dp, Color.Red)) {
red()
}
Row(Modifier.layoutId("blue").border(1.dp, Color.Blue), horizontalArrangement = Arrangement.End) {
blue()
}
Box(Modifier.layoutId("text").border(1.dp, Color.Magenta)) {
centeredText(TextAlign.Center)
}
},
) { measurables, constraints ->
val maxWidthCenteredTextShouldTake =
(constraints.maxWidth * maxWidthPercentageCenteredTextShouldTake).roundToInt()
val centeredTextConstraints = constraints.copy(
minWidth = 0,
maxWidth = maxWidthCenteredTextShouldTake,
)
val centerAlignedTextPlaceable = measurables.first { it.layoutId == "text" }.measure(centeredTextConstraints)
val redAndBlueWidth = (constraints.maxWidth - centerAlignedTextPlaceable.width) / 2
.coerceAtLeast(0)
val redAndBlueConstraints = constraints.copy(minWidth = 0, maxWidth = redAndBlueWidth)
val redPlaceable = measurables.first { it.layoutId == "red" }.measure(redAndBlueConstraints)
val bluePlaceable = measurables.first { it.layoutId == "blue" }.measure(redAndBlueConstraints)
val layoutWidth = constraints.maxWidth
val layoutHeight = listOf(centerAlignedTextPlaceable, redPlaceable, bluePlaceable)
.maxOf { it.height }
.coerceAtMost(constraints.maxHeight)
layout(layoutWidth, layoutHeight) {
redPlaceable.place(0, 0)
centerAlignedTextPlaceable.place(
(layoutWidth / 2) - (centerAlignedTextPlaceable.width / 2),
(layoutHeight / 2) - (centerAlignedTextPlaceable.height / 2),
)
bluePlaceable.place(layoutWidth - bluePlaceable.width, 0)
}
}
}
Can probably take this and adjust according to your needs.Stylianos Gakis
10/26/2022, 12:32 PM@Composable
fun BlueRedTextCentered() {
Column {
CustomLayout(
red = {
IconButton({}) {
Icon(Icons.Filled.ArrowBack, contentDescription = "Localized description")
}
},
centeredText = {
Text("Centered text....")
},
blue = {
IconButton({}) {
Icon(Icons.Filled.Favorite, contentDescription = "Localized description")
}
IconButton({}) {
Icon(Icons.Filled.Email, contentDescription = "Localized description")
}
},
)
Divider(Modifier.height(2.dp))
CustomLayout(
red = {
IconButton({}) {
Icon(Icons.Filled.ArrowBack, contentDescription = "Localized description")
}
},
centeredText = {
Text("Centered text....".repeat(5))
},
blue = {
IconButton({}) {
Icon(Icons.Filled.Favorite, contentDescription = "Localized description")
}
IconButton({}) {
Icon(Icons.Filled.Email, contentDescription = "Localized description")
}
},
)
Divider(Modifier.height(2.dp))
CustomLayout(
red = {
IconButton({}) {
Icon(Icons.Filled.ArrowBack, contentDescription = "Localized description")
}
},
centeredText = {
Text("Centered text....".repeat(8))
},
blue = {
IconButton({}) {
Icon(Icons.Filled.Favorite, contentDescription = "Localized description")
}
IconButton({}) {
Icon(Icons.Filled.Email, contentDescription = "Localized description")
}
IconButton({}) {
Icon(Icons.Filled.Phone, contentDescription = "Localized description")
}
IconButton({}) {
Icon(Icons.Filled.Check, contentDescription = "Localized description")
}
},
)
}
}
Results in pics below. Not perfect by any means but yeahDmitry Akishin
10/26/2022, 12:42 PMLukáš Kúšik
10/27/2022, 10:23 AM