Jetpack Compose - 使 LazyRow 中的第一个元素与屏幕中心对齐

发布于 2025-01-20 16:03:46 字数 411 浏览 0 评论 0原文

我想获得看起来像这样的lazyrow

| -aaa-b | bb-cccc | -dd ... ... | w-- x - x --- |

| ------- |是元素大小的一个屏幕宽度

各不相同,但它们之间的间距固定。 我以为我可以在lazyrow中添加一些开始内容填充,以便将“ AAA”组合对齐到屏幕中心,但我不知道其宽度。

如果您认为目前尚不清楚我要问什么,请发表评论。

更新

添加了一个GIF,以更好地理解

I want to obtain a LazyRow that looks like this:

|--aaa-b|bb-cccc|-dd... ...|w--x---|

|-------| is one screen width

The size of the elements varies but they have a fixed spacing between them.
I thought I could add some start content padding to the LazyRow so that the "aaa" Composable is aligned to the center of the screen, but I don't know its width.

If you think it's not clear what I'm asking, please drop a comment.

UPDATE

Added a gif for better understanding

enter image description here

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

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

发布评论

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

评论(2

各自安好 2025-01-27 16:03:46

您可以使用boxwithConstraints获取屏幕宽度。然后,您可以使用布局将项目正确定位在列表中。

@Composable
fun BigCarousel() {
    val items = (0..10).map { "Item $it" }
    BoxWithConstraints {
        LazyRow {
            itemsIndexed(items) { index, item ->
                Layout(
                    content = {
                        // Here's the content of each list item.
                        Box(
                            Modifier
                                .size(200.dp)
                                .padding(8.dp)
                                .background(Color.Gray)
                        ) {
                            Text(text = item, Modifier.align(Alignment.Center))
                        }
                    },
                    measurePolicy = { measurables, constraints ->
                        // I'm assuming you'll declaring just one root 
                        // composable in the content function above
                        // so it's measuring just the Box
                        val placeable = measurables.first().measure(constraints)
                        // maxWidth is from the BoxWithConstraints
                        val maxWidthInPx = maxWidth.roundToPx()
                        // Box width
                        val itemWidth = placeable.width
                        // Calculating the space for the first and last item
                        val startSpace =
                            if (index == 0) (maxWidthInPx - itemWidth) / 2 else 0
                        val endSpace =
                            if (index == items.lastIndex) (maxWidthInPx - itemWidth) / 2 else 0
                        // The width of the box + extra space
                        val width = startSpace + placeable.width + endSpace
                        layout(width, placeable.height) {
                            // Placing the Box in the right X position
                            val x = if (index == 0) startSpace else 0
                            placeable.place(x, 0)
                        }
                    }
                )
            }
        }
    }
}

这是结果:

You can use the BoxWithConstraints to get screen width. Then you can use Layout to properly positioning the item in the list.

@Composable
fun BigCarousel() {
    val items = (0..10).map { "Item $it" }
    BoxWithConstraints {
        LazyRow {
            itemsIndexed(items) { index, item ->
                Layout(
                    content = {
                        // Here's the content of each list item.
                        Box(
                            Modifier
                                .size(200.dp)
                                .padding(8.dp)
                                .background(Color.Gray)
                        ) {
                            Text(text = item, Modifier.align(Alignment.Center))
                        }
                    },
                    measurePolicy = { measurables, constraints ->
                        // I'm assuming you'll declaring just one root 
                        // composable in the content function above
                        // so it's measuring just the Box
                        val placeable = measurables.first().measure(constraints)
                        // maxWidth is from the BoxWithConstraints
                        val maxWidthInPx = maxWidth.roundToPx()
                        // Box width
                        val itemWidth = placeable.width
                        // Calculating the space for the first and last item
                        val startSpace =
                            if (index == 0) (maxWidthInPx - itemWidth) / 2 else 0
                        val endSpace =
                            if (index == items.lastIndex) (maxWidthInPx - itemWidth) / 2 else 0
                        // The width of the box + extra space
                        val width = startSpace + placeable.width + endSpace
                        layout(width, placeable.height) {
                            // Placing the Box in the right X position
                            val x = if (index == 0) startSpace else 0
                            placeable.place(x, 0)
                        }
                    }
                )
            }
        }
    }
}

Here's the result:

enter image description here

上课铃就是安魂曲 2025-01-27 16:03:46

计算运行时间的设备宽度,然后除以2或您想要的任何比率。

检查此链接的运行时间宽度计算: https://stackoverflow.com/a/11755265/7248394

开始。

Lazycolumn(
contentpadding = paddingvalues(start =(device_width/2).dp)

这将在所有设备中保持不变。

Calculate the device width in run time and divide by 2 or whatever ratio you want.

check this link for run time width calcuation: https://stackoverflow.com/a/11755265/7248394

set the content padding start.

LazyColumn(
contentPadding = PaddingValues(start = (device_width/2).dp)
)

This will remain same across all devices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文