如何将 Jetpack Compose 中的子项传递给自定义可组合项?
How to pass children in Jetpack Compose to a custom composable?
我很好奇是否可以将可组合项传递给自定义可组合项块。然后在其定义中呈现。我在想可以采用 vararg + 函数文字方法,但找不到任何信息。
//definition
@Composable
fun Content() {
Row(modifier = Modifier.fillMaxWidth()) {
//insert a(), b(), ..., z() so that they render in the row
}
}
//usage
Content() {
a()
b()
...
z()
}
这样的东西已经存在了吗?您可以通过这种方式使用 Jetpack Compose。行实现必须以某种方式处理文本。
Row(){
Text("a")
Text("b")
Text("c")
}
在查看 Row 和 RowScope 的实现后,发现 documentation. This can be achieved by the following code sample. The content 类型为 @Composable() () -> Unit
的函数参数被传递到行中。
//definition
@Composable
fun MyCustomContent(
modifier: Modifier = Modifier,
content: @Composable() () -> Unit
) {
Row(modifier = modifier) {
content()
}
}
//usage
MyCustomContent() {
a()
b()
z()
}
我很好奇是否可以将可组合项传递给自定义可组合项块。然后在其定义中呈现。我在想可以采用 vararg + 函数文字方法,但找不到任何信息。
//definition
@Composable
fun Content() {
Row(modifier = Modifier.fillMaxWidth()) {
//insert a(), b(), ..., z() so that they render in the row
}
}
//usage
Content() {
a()
b()
...
z()
}
这样的东西已经存在了吗?您可以通过这种方式使用 Jetpack Compose。行实现必须以某种方式处理文本。
Row(){
Text("a")
Text("b")
Text("c")
}
在查看 Row 和 RowScope 的实现后,发现 documentation. This can be achieved by the following code sample. The content 类型为 @Composable() () -> Unit
的函数参数被传递到行中。
//definition
@Composable
fun MyCustomContent(
modifier: Modifier = Modifier,
content: @Composable() () -> Unit
) {
Row(modifier = modifier) {
content()
}
}
//usage
MyCustomContent() {
a()
b()
z()
}