如何开始填充 LazyVerticalGrid 中心的项目?

huangapple go评论84阅读模式
英文:

How to start filling items for the center in LazyVerticalGrid?

问题

我正在尝试在Jetpack Compose中实现一个惰性网格,以便项目在水平和垂直方向上对齐。以下是我的代码:

@Preview(showBackground = true, widthDp = 320, heightDp = 320)
@Composable
private fun ModuleGrid() {
    val modules = ModuleRegistry.getModulesList().toMutableList()
    Box(
        contentAlignment = Alignment.Center
    ) {
        LazyVerticalGrid(
            modifier = Modifier.fillMaxHeight().widthIn(120.dp),
            columns = GridCells.Adaptive(minSize = 96.dp),
            horizontalArrangement = Arrangement.Center,
            verticalArrangement = Arrangement.Center
        ) {
            items(modules, key = { it.namedTag }) {
                Card(
                    colors = CardDefaults.cardColors(
                        containerColor = Color.Transparent
                    ),
                    modifier = Modifier
                        .size(120.dp)
                        .background(Color.Transparent)
                        .clickable {
                            launchModule(it)
                        }
                ) {
                    Column(
                        modifier = Modifier.fillMaxSize(),
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        it.displayIcon?.let { icon ->
                            Image(
                                painter = painterResource(id = icon),
                                contentDescription = it.displayName,
                                contentScale = ContentScale.Fit,
                                modifier = Modifier
                                    .size(96.dp)
                                    .padding(4.dp)
                                    .align(Alignment.CenterHorizontally)
                            )
                        }
                        Text(text = it.displayName, modifier = Modifier.align(Alignment.CenterHorizontally))
                    }
                }
            }
        }
    }
}

输出结果是 截图

这个项目应该显示在屏幕的中央。

英文:

I am trying to implement a lazy grid in jetpack compose so that items are horizontally and vertically aligned, below is my code

@Preview(showBackground = true, widthDp = 320, heightDp = 320)
    @Composable
    private fun ModuleGrid() {
        val modules = ModuleRegistry.getModulesList().toMutableList()
        Box(
            contentAlignment = Alignment.Center) {
            LazyVerticalGrid(
                modifier = Modifier.fillMaxHeight().widthIn(120.dp),
                columns = GridCells.Adaptive(minSize = 96.dp),
                horizontalArrangement = Arrangement.Center,
                verticalArrangement = Arrangement.Center
            ) {
                
                items(modules,
                    key = { it.namedTag }
                ) {
                    Card(
                        colors = CardDefaults.cardColors(
                            containerColor = Color.Transparent
                        ),
                        modifier = Modifier
                            .size(120.dp)
                            .background(Color.Transparent)
                            .clickable {
                                launchModule(it)
                            }
                    ) {
                        Column(
                            modifier = Modifier.fillMaxSize(),
                            verticalArrangement = Arrangement.Center,
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            it.displayIcon?.let { icon ->
                                Image(
                                    painter = painterResource(id = icon),
                                    contentDescription = it.displayName,
                                    contentScale = ContentScale.Fit,
                                    modifier = Modifier
                                        .size(96.dp)
                                        .padding(4.dp)
                                        .align(Alignment.CenterHorizontally)
                                )
                            }
                            Text(text = it.displayName, modifier = Modifier
                                .align(Alignment.CenterHorizontally))
                        }
                    }
                }
            }
        }
    }

The output result is screenshot

This item should be displayed in the centre of the screen.

如何开始填充 LazyVerticalGrid 中心的项目?
如何开始填充 LazyVerticalGrid 中心的项目?

I tried using the horizontal grid but ended up getting a similar result.

答案1

得分: 1

如果您想要每列只使用一个项目,而不是自适应项目,请使用 GridCells.Fixed,这里是一个示例,使LazyVerticalGrid中的项目位于屏幕中央:

Box(
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Alignment.Center
) {
    LazyVerticalGrid(
        columns = GridCells.Fixed(1), // 列数
        contentPadding = PaddingValues(horizontal = 100.dp, vertical = 8.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp),
        horizontalArrangement = Arrangement.Center,
        modifier = Modifier.fillMaxHeight()
    ) {
        items(20) {
            Box(
                modifier = Modifier
                    .size(120.dp)
                    .background(Color.Gray)
            ) {
                Text(
                    text = "项目 $it",
                    color = Color.White,
                    fontSize = 20.sp,
                    modifier = Modifier.align(Alignment.Center)
                )
            }
        }
    }
}

希望这对您有帮助!

英文:

if you want to use only one item per column remove items to be adaptive instead use GridCells.Fixed here is an example to make LazyVerticalGrid items in the center of the screen:

    Box(
    modifier = Modifier.fillMaxSize(),
    contentAlignment = Alignment.Center
) {
        LazyVerticalGrid(
            columns = GridCells.Fixed(1), // number of columns
            contentPadding = PaddingValues(horizontal = 100.dp, vertical = 8.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier.fillMaxHeight()
        ) {
            items(20) {
                Box(
                    modifier = Modifier
                        .size(120.dp)
                        .background(Color.Gray)
                ) {
                    Text(
                        text = "Item $it",
                        color = Color.White,
                        fontSize = 20.sp,
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
            }
        }
    }

huangapple
  • 本文由 发表于 2023年3月9日 18:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定