我需要实施下一个网格:

enter image description here

红色方框的大小应取决于屏幕宽度.我try 使用列和行:

@Composable
fun Component() {
    Column(modifier = Modifier.fillMaxWidth()) {
        Row {
            repeat(5) {
                if (it > 0) {
                    Spacer(modifier = Modifier.width(20.dp))
                }
                Box(modifier = Modifier
                    .aspectRatio(1f)
                    .weight(1f).background(Color.Red))
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Row {
            repeat(4) {
                if (it > 0) {
                    Spacer(modifier = Modifier.width(20.dp))
                }
                val weight = if (it < 3) 1f else 2f
                Box(modifier = Modifier
                    .aspectRatio(weight)
                    .weight(weight).background(Color.Red))
            }
        }
    }
}

但是因为我在第二排少了一个位子,所以看起来并不完美.

enter image description here

如何用合并的单元格创建像素完美的网格视图? 我知道LazyGrid,但我不确定它是否合适,因为我的网格需要全屏显示.

推荐答案

您可以使用LazyGridScope DSL itemitems方法的103参数指定column span.

类似于:

val listgrid = (0 until 9).toList()

LazyVerticalGrid(
    columns = GridCells.Fixed(5),
    horizontalArrangement = Arrangement.spacedBy(16.dp),
    verticalArrangement = Arrangement.spacedBy(16.dp)
) {


    items(listgrid,
        span = { index ->
            val spanCount = if (index == 8) 2 else 1
            GridItemSpan(spanCount)
        }
    ) {

        Box(Modifier.size(50.dp).background(Red, RoundedCornerShape(4.dp)))

    }
}

enter image description here

Android相关问答推荐

如何解决Android Studio中的in fragment问题

如何检测HitTest是否命中给定的网格对象?

如何从Android 12的来电中获取电话号码?

在Jetpack Compose中的隐藏状态栏后面绘制

如何在Jetpack Compose中更新异步回调的用户界面

如何从sqlite数据库中检索数据到碎片android?

如何用帆布在喷气背包中画一个圆环?

在Android中使用Room从SQlite数据库中获取实体列表的正确方式是什么?

Kotlin为多个控件设置一个侦听器

未解析的引用:视图模型

我需要在房间数据库中保留旧的自动迁移行吗?

使用不同的gradle文件导入外部库

如何在 Jetpack Compose 中对齐按钮底部中心?

如何以编程方式通过 whatsapp android 共享图像和文本

在 Jetpack Compose 中使用 ViewModel 实现 startActivity 的最佳实践

从活动共享视图模型以使用 hilt 组合函数

PullRefreshIndicator 与 ScrollableTabRow 重叠

Jetpack Compose UI - 在 AlertDialog 中单击时按钮宽度会发生变化

Compose Accompaniist Pager 中的 TabRow/Tab 重组问题

如何使用 Kotlin 在 Android Wear(Galaxy watch 4)中继续在后台运行应用程序