kotlin jetpack在懒惰的列中撰写中心文本

发布于 2025-02-05 15:44:27 字数 2306 浏览 3 评论 0原文

我是Jetpack组成的新手,我真的很喜欢。但是遇到了一个问题:我想创建一张卡,在其中,我想显示一个带有分隔线的项目列表。我几乎能够在这里实现它是我的代码:

Box(modifier = Modifier.background(Color(0xFFf4f4f4))
    .fillMaxSize()
    .padding(top = 20.dp)) {
    Card(
        modifier = Modifier
            .fillMaxWidth(),
        shape = RoundedCornerShape(20.dp),
        elevation = 5.dp
    ) {
        val colorNamesList = listOf("Red", "Green", "Blue", "Indigo")
        LazyColumn() {
            itemsIndexed(
                colorNamesList,
            ) { index, item ->
                Column(
                    modifier = Modifier
                        .background(
                            color = Color(0xFFf2f2f2),
                        )
                        .clickable {
                            println(item)
                        },
                    horizontalAlignment = Alignment.CenterHorizontally,//those 2 does nothing 
                    verticalArrangement = Arrangement.Center //when i change it nothing change
                ) {
                    println(item + index)
                    Text(
                        text = "Item at  $item",
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(50.dp),
                        //textAlign = TextAlign.Center, without it image 1, with it image 2
                        color = Color.Black,
                    )
                    if (index < colorNamesList.lastIndex) {
                        Divider(
                            color = Color.Black.copy(alpha = 0.2f),
                            modifier = Modifier
                                .padding(horizontal = 80.dp),
                        )
                    }
                }
            }
        }
    }
}

它几乎很好,但是我没有设法对齐其中的文本,我搜索了一些答案,但是对齐和安排在我的情况下并没有改变任何东西。

我能够通过添加textalign = textalign.center水平对齐,就像我在代码中所说的那样,但是,我无法垂直对齐,文本留在他的单元格的顶部:

我想做的是列的每个单元格中的文本中心。感谢您的帮助。

I'am new to jetpack compose and i really liked it. But ran into a problem : I want to create a card and inside of it, i would like to display a list of item with divider between them. I was almost able to achieved it here is my code :

Box(modifier = Modifier.background(Color(0xFFf4f4f4))
    .fillMaxSize()
    .padding(top = 20.dp)) {
    Card(
        modifier = Modifier
            .fillMaxWidth(),
        shape = RoundedCornerShape(20.dp),
        elevation = 5.dp
    ) {
        val colorNamesList = listOf("Red", "Green", "Blue", "Indigo")
        LazyColumn() {
            itemsIndexed(
                colorNamesList,
            ) { index, item ->
                Column(
                    modifier = Modifier
                        .background(
                            color = Color(0xFFf2f2f2),
                        )
                        .clickable {
                            println(item)
                        },
                    horizontalAlignment = Alignment.CenterHorizontally,//those 2 does nothing 
                    verticalArrangement = Arrangement.Center //when i change it nothing change
                ) {
                    println(item + index)
                    Text(
                        text = "Item at  $item",
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(50.dp),
                        //textAlign = TextAlign.Center, without it image 1, with it image 2
                        color = Color.Black,
                    )
                    if (index < colorNamesList.lastIndex) {
                        Divider(
                            color = Color.Black.copy(alpha = 0.2f),
                            modifier = Modifier
                                .padding(horizontal = 80.dp),
                        )
                    }
                }
            }
        }
    }
}

It's almost good but i didn't managed to align the text inside it, i searched some answer but Alignment and Arrangement doesn't change anything in my situation.
image 1

I was able to align horizontally by adding textAlign = TextAlign.Center as i commented in my code but, i couldn't align vertically, the text stay on the top of his cell :
Image 2

What i want to do is text center in every cell of the column. Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2025-02-12 15:44:28

您可以通过添加类似的XML属性(例如“ wrap_content” )作为高度来实现这一目标。

添加“ wrapcontentheight()” 到修饰符,您将其使其与页面的中心保持一致。

Text(
                    text = "Item at  $item",
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .wrapContentHeight(),
                    textAlign = TextAlign.Center
                    color = Color.Black,
                )

You can achieve this by adding similar xml attribute like "wrap_content" as height in compose too.

Add "wrapContentHeight()" to modifier and you will get it to align to centre of the page.

Text(
                    text = "Item at  $item",
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .wrapContentHeight(),
                    textAlign = TextAlign.Center
                    color = Color.Black,
                )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文