Android 粘性 - 使用 jetpack 撰写的页脚:将页脚视图与表格对齐,直到达到屏幕尺寸,然后固定在底部

Bpn*_*Bpn 10 android sticky-footer android-jetpack-compose android-compose-lazyfor

我想使用 jetpack compose 来实现这一点。

在此输入图像描述

A是行项目的可滚动列表。当A小于屏幕(或父级)尺寸时,B(页脚)应放置在最后一行下方。当A + B大于屏幕尺寸时,B固定在底部,A内容可滚动。我想知道是否有简单的方法可以使用 compose ConstraintLayout 来实现此目的。

Bpn*_*Bpn 17

解决方案是在A中使用Modifier.weight(1f, false)。这是因为父元素的大小将首先被未加权的元素使用,然后剩余的空间将根据其权重分配给加权的元素。有关更多详细信息,请参阅此答案。


Pun*_*rma 10

有很多方法可以做到这一点,但最有效的方法是使用ViewbottomBar的属性Scaffold

例如:

@Composable
fun RenderContent() {
    Scaffold(
        topBar = { /* Your app bar goes here */ },
        bottomBar = { /* Anything you place in here will be stick to the bottom. */ }
    ) {
        // ... Rest of the content
        // Benefits: If you make the content scrollable and
        // the `bottomBar` composable remain on top of the content
    }
}
Run Code Online (Sandbox Code Playgroud)