我正在尝试在JetPack中创建一个列,并在每个子元素之间具有特定数量的间距,例如10.dp。在Swiftui中,这相对简单。我只会创建一个VStack
与spacing
价值:
struct ContentView: View {
let strings = ["Hello", "World", "Apples"]
var body: some View {
VStack(spacing: 10) {
ForEach(strings, id: \.self) { string in
Text(string).background(Color.green)
}
}
.background(Color.blue)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这就是Swiftui预览中的样子:
在JetPack组成中,我的代码是:
@Composable
fun ContentView() {
val strings = listOf<String>("Hello", "World", "Apples")
MaterialTheme {
Surface(color = MaterialTheme.colors.background) {
Column(
modifier = Modifier
.background(Color.Blue)
// I would expect to be able to add something like ".spacer(10.dp)" here
) {
strings.forEach { string ->
Text(
modifier = Modifier.background(Color.Green),
text = string
)
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
ContentView()
}
因为此代码缺少任何间距,所以这就是当前的样子:
My criteria are:
- A space of a specific height (such as 10.dp) should be present between child elements of a Column.
- The space should not appear before the first element.
- The space should not appear after the last element.
我会用Spacer
元素,但我不相信这会起作用,因为在循环中,我相信我最终会有一个Spacer
我不想要的第一个元素或最后一个元素之前。