android - 如何使用 Jetpack Compose 创建 GridView

标签 android android-jetpack-compose android-jetpack-compose-list

如何在不使用回收 View 或 android.widget.gridview 的情况下在 Jetpack compose 中创建 Gridview?

最佳答案

1.x.y LazyVerticalGrid composable 为在网格中显示项目提供实验性支持

val numbers = (0..20).toList()

LazyVerticalGrid(
    columns = GridCells.Fixed(4)
) {
    items(numbers.size) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = "Number")
            Text(text = "  $it",)
        }
    }
}

columns = GridCells.Fixed(4) 意味着有 4 列占父级宽度的 1/4。

enter image description here

val numbers = (0..20).toList()

LazyVerticalGrid(
    columns = GridCells.Adaptive(minSize = 64.dp)
) {
    items(numbers) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = "Number")
            Text(text = "  $it",)
        }
    }
}

columns = GridCells.Adaptive(minSize = 64.dp) 意味着将有尽可能多的列,每列至少为 64.dp,并且所有列都将相等宽度。

enter image description here

关于android - 如何使用 Jetpack Compose 创建 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58596541/

相关文章:

android - Android dex 中的冗余操作码

android - Jetpack 撰写 : Content color of TabRow

相对布局中的 Android ScrollView 问题

android - 默认样式 Jetpack 组合

android - 如何在 Android Studio 中的模块中使用不同的 Kotlin 版本(WearOS 模块)

android - 未能应用插件 'com.android.internal.library' 。 Android Gradle 插件需要 Java 11 才能运行。您当前使用的是 Java 1.8

android - Jetpack Compose 中的 LazyList 内存泄漏

android - 撰写 LazyList 部分背景

javascript - 在移动网络应用程序上触发滑动和触摸事件? - iPhone/安卓

android - Android中的动态启动 Activity ?