来自 SwiftUI,我想创建一个视图Text,它有一个圆的背景,其中圆的宽度/高度随着里面的文本Text变长而增长。
因为Circle()Compose 中没有像 in 那样SwifUI,所以我只创建了一个Spacer并剪掉了它。下面的代码正在使用,ConstraintLayout因为我不知道如何获得宽度Text以将Circle可组合的大小设置为相同:
@Composable
fun CircleDemo() {
ConstraintLayout {
val (circle, text) = createRefs()
Spacer(
modifier = Modifier
.background(Color.Black)
.constrainAs(circle) {
centerTo(text)
}
)
Text(
text = "Hello",
color = Color.White,
modifier = Modifier
.constrainAs(text) {
centerTo(parent)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用一个mutableStateOf { 0 }我在onGloballyPositioned附加到的修饰符中更新它的地方Text,然后将其设置为requiredSizefor Spacer,但是 1. 这看起来很愚蠢,2.Spacer现在绘制在ConstraintLayout. …
android android-jetpack-compose android-jetpack-compose-text