英文:
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.
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)
)
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论