Jetpack Compose - 使 LazyRow 中的第一个元素与屏幕中心对齐
我想获得看起来像这样的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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
boxwithConstraints
获取屏幕宽度。然后,您可以使用布局
将项目正确定位在列表中。这是结果:
You can use the
BoxWithConstraints
to get screen width. Then you can useLayout
to properly positioning the item in the list.Here's the result:
计算运行时间的设备宽度,然后除以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.