如何在JetPack组成的柱中指定儿童之间的确切间距?


我正在尝试在JetPack中创建一个列,并在每个子元素之间具有特定数量的间距,例如10.dp。在Swiftui中,这相对简单。我只会创建一个VStackspacing价值:

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我不想要的第一个元素或最后一个元素之前。


使用Arrangement.spacedBy


Column(
    modifier = Modifier
        .background(Color.Blue),
    verticalArrangement = Arrangement.spacedBy(10.dp)
) {
    // content here
}
  

如何在JetPack组成的柱中指定儿童之间的确切间距?的相关文章

随机推荐