android 在Jetpack Compose中考虑项目高度偏移项目

cyej8jka  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(99)

我想垂直放置我自己的组件(比如一个文本组件),这样我就可以指定相对于文本组件底部的y偏移量。我如何在Jetpack合成中做到这一点?
比如说

Column {
    Text("Something", modifier = Modifier.offset(y=10.dp))
}

但是10dp不是代表文本组件的顶部y位置,而是底部y位置。基本上考虑了文本的高度,即使文本大小改变。所以y = offset.y - height
在我看来有两个问题
1.字体大小是可以改变的,所以我不能硬编码的文字高度。
1.我需要知道我的文本组件在合成过程中的大小,但我不知道如何得到它。

ajsxfq5m

ajsxfq5m1#

你可以选择定制的可组合产品

@Composable
fun CustomText(y: Dp){
    Layout(content = { Text(text = "Lorem Ipsum") }){measurables, constraints ->
        val text = measurables[0].measure(constraints)
        layout(constraints.maxWidth, constraints.maxHeight){ //Change these per your needs
            text.placeRelative(IntOffset(0, y.value.roundToInt() - text.height))
        }
    }
}

您也可以使用自定义修饰符。使用layout修饰符 checkout

相关问题