ConstraintSet

박채빈·2024년 1월 25일
0

AndroidStudy

목록 보기
9/19
post-thumbnail
@Composable
fun ConstraintSetEx() {
    val constraintSet = ConstraintSet {
        // createRefFor 를 사용해 red, magenta, green, yellow 레퍼런스 만들기 (id 적용)
        val redBox = createRefFor("redBox")
        val magentaBox = createRefFor("magentaBox")
        val greenBox = createRefFor("greenBox")
        val yellowBox = createRefFor("yellowBox")

        // constrain을 열고 위에 생성한 인자들 넣어줌
        // 각각의 레퍼런스에 제약조건 부여
        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)
        }
    }

    // constraint 연결
    ConstraintLayout(
        constraintSet,
        modifier = Modifier.fillMaxSize())
    {
        // Box마다 layoutId 설정
        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")
        )
    }
}

profile
안드로이드 개발자

0개의 댓글