@Composable
fun ConstraintSetEx() {
val constraintSet = ConstraintSet {
val redBox = createRefFor("redBox")
val magentaBox = createRefFor("magentaBox")
val greenBox = createRefFor("greenBox")
val yellowBox = createRefFor("yellowBox")
constrain(redBox) {
bottom.linkTo(parent.bottom, margin = 10.dp)
end.linkTo(parent.end, margin = 30.dp)
}
constrain(magentaBox) {
start.linkTo(parent.start)
end.linkTo(parent.end)
}
constrain(greenBox) {
centerTo(parent)
}
constrain(yellowBox) {
start.linkTo(greenBox.end)
top.linkTo(greenBox.bottom)
}
}
ConstraintLayout(
constraintSet,
modifier = Modifier.fillMaxSize())
{
Box(modifier = Modifier
.size(40.dp)
.background(color = Color.Red)
.layoutId("redBox")
)
Box(modifier = Modifier
.size(40.dp)
.background(color = Color.Magenta)
.layoutId("magentaBox")
)
Box(modifier = Modifier
.size(40.dp)
.background(color = Color.Green)
.layoutId("greenBox")
)
Box(modifier = Modifier
.size(40.dp)
.background(color = Color.Yellow)
.layoutId("yellowBox")
)
}
}
![](https://velog.velcdn.com/images/parkchaebin/post/70dbadd6-a588-41b4-98a6-2f7a81685cdd/image.png)