Christopher Mederos
05/17/2026, 9:23 AMChristopher Mederos
05/17/2026, 10:36 AM@Composable
fun Example() {
val colors = listOf(Color.DarkGray, Color.LightGray)
val height = 32.dp
val n = 72
Column(Modifier.fillMaxWidth()) {
// Weights
Row(Modifier.fillMaxWidth()) {
for (i in 0 until n) {
Box(Modifier.weight(1f).height(height).background(colors[i.mod(2)])) {}
}
}
// size.width / n
Box(modifier = Modifier.fillMaxWidth().drawWithCache {
val segmentWidth = size.width / (n)
onDrawBehind {
for (i in 0 until n) {
drawRect(
color = colors[i.mod(2)],
topLeft = Offset(i * segmentWidth, 0f),
size = Size(width = segmentWidth, height = height.toPx())
)
}
}
})
}
}
Though now I'm wondering if it's just a bit of luck that the two methods are aligned on Android (device size, density, etc). Perhaps the segment width on the canvas should implement the same rounding logic as the modifier.weight() under the hoodJonathan
05/18/2026, 1:54 PM