I am trying to use Compose's ConstraintLayout
composable to place a composable on 30% midway through the screen from the top.
I tried using the pivotY
attribute in the constraint DSL to no avail, translateY
worked but I don't want to specify Dp for obvious reasons)
Here is a test composable I've tested with
@Composable
@Preview
fun Test() {
ConstraintLayout(
modifier = Modifier.fillMaxWidth().aspectRatio(1f)
) {
val btn = createRef()
Box(
modifier = Modifier
.size(64.dp)
.background(Color.Red)
.constrainAs(btn) {
bottom.linkTo(parent.bottom)
top.linkTo(parent.top)
pivotY = 30f // or -30f, none is working (same thing with 0.3f of -0.3f)
}
)
}
}
I've found the answer in here.
found out i have to use another linkTo
function
@Composable
@Preview
fun Test() {
ConstraintLayout(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f),
) {
val btn = createRef()
Box(
modifier = Modifier
.size(64.dp)
.background(Color.Red)
.constrainAs(btn) {
linkTo(parent.top, parent.bottom, bias = 0.3f)
},
)
}
}