Art*_*mes 10 android android-jetpack-compose android-jetpack-compose-list
我正在尝试实现一个底部带有按钮的列表,如下所示:
由于我不知道列表中会有多少项目,因此我使用LazyColumn. 仅当列表没有填满整个屏幕时,才应将底部的按钮放置在那里,如果填满整个屏幕,则该按钮应向下移动并成为列表中的最后一项。
像这样移动按钮LazyColumn:
LazyColumn(...) {
items(items) { item -> ...}
item { Button() }
}
Run Code Online (Sandbox Code Playgroud)
给出以下内容:
我尝试在两者之间添加一个SpacerwithfillMaxHeight()修饰符作为项目,但它没有改变。
我还尝试在列中添加LazyColumn和:Button
Column {
LazyColumn(
modifier = Modifier.weight(1f)
) {
items(items) { item -> ...}
}
Button()
}
Run Code Online (Sandbox Code Playgroud)
但这仅将按钮固定在底部,就好像它Column是一个LinearLayout.
考虑到这一点,是否可以对齐其中一个LazyColumn项目,使其始终位于底部?或者,添加某种空间来填充可用区域?
Art*_*mes 10
如本已关闭问题中所述,可以使用LazyColumnsverticalArrangement参数通过实现来对齐底部的最后一项Arrangement.Vertical:
LazyColumn(verticalArrangement = remember {
object : Arrangement.Vertical {
override fun Density.arrange(
totalSize: Int,
sizes: IntArray,
outPositions: IntArray
) {
var currentOffset = 0
sizes.forEachIndexed { index, size ->
if (index == sizes.lastIndex) {
outPositions[index] = totalSize - size
} else {
outPositions[index] = currentOffset
currentOffset += size
}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
spacedBy如果你想像我一样在参数上使用它verticalArrangement,你可以使用以下内容:
fun spacedByWithFooter(space: Dp) = object : Arrangement.Vertical {
override val spacing = space
override fun Density.arrange(
totalSize: Int,
sizes: IntArray,
outPositions: IntArray,
) {
if (sizes.isEmpty()) return
val spacePx = space.roundToPx()
var occupied = 0
var lastSpace = 0
sizes.forEachIndexed { index, size ->
if (index == sizes.lastIndex) {
outPositions[index] = totalSize - size
} else {
outPositions[index] = min(occupied, totalSize - size)
}
lastSpace = min(spacePx, totalSize - outPositions[index] - size)
occupied = outPositions[index] + size + lastSpace
}
occupied -= lastSpace
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5442 次 |
| 最近记录: |