Skip to content

Instantly share code, notes, and snippets.

@jisungbin
Created March 17, 2023 13:07
Show Gist options
  • Save jisungbin/b30e425a3f42a148903069e635e35724 to your computer and use it in GitHub Desktop.
Save jisungbin/b30e425a3f42a148903069e635e35724 to your computer and use it in GitHub Desktop.
class PlaygroundActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Layout(
modifier = Modifier.fillMaxSize(),
content = {
Box(
modifier = Modifier
.layoutId("green")
.size(100.dp)
.background(color = Color.Green)
)
Box(
modifier = Modifier
.layoutId("red")
.size(100.dp)
.background(color = Color.Red)
)
Box(
modifier = Modifier
.layoutId("blue")
.size(100.dp)
.background(color = Color.Blue)
)
},
measurePolicy = { measurables: List<Measurable>, constraints: Constraints ->
val looseConstaints = constraints.copy(
minHeight = 150.dp.roundToPx(),
minWidth = 100.dp.roundToPx(),
)
val blueMeasurable = measurables.find { it.layoutId == "blue" }!!
val greenMeasurable = measurables.find { it.layoutId == "green" }!!
val redMeasurable = measurables.find { it.layoutId == "red" }!!
val bluePlaceable = blueMeasurable.measure(looseConstaints)
val redPlaceable = redMeasurable.measure(looseConstaints)
val greenPlaceable = greenMeasurable.measure(looseConstaints)
layout(width = constraints.minWidth, height = constraints.minHeight) {
val y = 100
bluePlaceable.place(x = 100, y = y)
redPlaceable.place(x = 100, y = y + bluePlaceable.height)
greenPlaceable.place(x = 100, y = y + bluePlaceable.height + redPlaceable.height)
}
},
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment