You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除Jetpack Compose中LazyVerticalGrid的项间距?

移除Jetpack Compose LazyVerticalGrid的项间距

要消除LazyVerticalGrid中各项之间的空白,只需显式设置组件的水平排列和垂直排列参数为无间距即可——默认的排列方式会自带间距。

修改后的完整代码如下:

val list = mutableListOf("lorem", "ipsum", "dolor", "sit")

@Composable
fun MyUi() {
    LazyVerticalGrid(
        columns = GridCells.Fixed(2),
        // 消除水平方向的项间距
        horizontalArrangement = Arrangement.spacedBy(0.dp),
        // 消除垂直方向的项间距
        verticalArrangement = Arrangement.spacedBy(0.dp),
        content = {
            items(list.size) { index ->
                Text(text = list[index])
            }
        })
}

关键说明

  • horizontalArrangement = Arrangement.spacedBy(0.dp):控制同一行内两个子项的水平间距,设为0即可移除默认的水平空白
  • verticalArrangement = Arrangement.spacedBy(0.dp):控制不同行之间的垂直间距,设为0即可移除默认的垂直空白
  • 如果你的子组件(比如示例中的Text)有自定义的padding或margin,也需要同步移除,避免额外间距

内容的提问来源于stack exchange,提问作者Jorpy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:02:37