Bar*_*fet 6 android android-jetpack-compose
我知道如何获取按钮/或组件的 X,Y 坐标,但我想知道每个角的偏移量或至少是右下角的偏移量。例如,我从以下位置获取左上角坐标:
Modifier.onGloballyPositioned{
it.positionInRoot().x
it.positionInRoot().y
}
Run Code Online (Sandbox Code Playgroud)
有没有办法知道右下角坐标,以便我可以计算组件内部的差异偏移?
编辑:可能的解决方案 - >在组件旁边使用间隔符:让我们想象一下我们想要获得按钮的右下角。
Row(
horizontalArrangement = Arrangement.Center,
modifier = Modifier
.fillMaxWidth()
) {
Button(
modifier = Modifier
.onGloballyPositioned {
it.positionInRoot().x
it.positionInRoot().y
}
) {...}
Row(modifier =Modifier.fillMaxHeight(),verticalAlignment = Alignment.Bottom) {
Spacer(
Modifier
.height(0.dp)
.width(0.dp)
.onGloballyPositioned {
it.positionInRoot().x
it.positionInRoot().y
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*hov 11
看起来您正在寻找LayoutCoordinates.boundsInRoot()
Modifier.onGloballyPositioned { layoutCoordinates ->
val rect = layoutCoordinates.boundsInRoot()
println("${rect.topLeft} ${rect.bottomRight}")
}
Run Code Online (Sandbox Code Playgroud)
大多数时候,您不需要在 Compose 中进行布局
您可以将画布和视图都放置在 中Box,matchParentSize向画布添加修饰符,它将与视图的大小相同。在里面Canvas你可以使用size、center等参数来绘制
Box {
Canvas(Modifier.matchParentSize()) {
// here you can access size, center, etc
drawRect(Color.Green)
}
Text("hello")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6830 次 |
| 最近记录: |