避免在 jetpack 组合中使用 LazyColumn 的特定子项进行侧边填充

Viv*_*odi 2 android kotlin android-jetpack android-jetpack-compose android-jetpack-compose-layout

我想删除 中特定子项目的侧边填充LazyColum我在这篇文章的帮助下用 xml 解决了这个问题。我在jetpack compose中有同样的场景。我正在使用Material 3compose_bom = "2022.11.00"的BOM 版本。

    Card(shape = RoundedCornerShape(6.dp),) {
        Column(modifier.background(Color.White)) {
            LazyColumn(
                contentPadding = PaddingValues(all =16.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp),
            ) {
                item {
                    Text(
                        text = "Device Header",
                        modifier = Modifier.padding(top = 10.dp),
                        style = headerTextStyle
                    )
                }

                item {
                    Divider() // remove padding from side in here
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

实际产量

在此输入图像描述

预期输出

在此输入图像描述

Gab*_*tti 5

在 Compose 中,您不能在子容器中使用负填充来减少父容器应用的填充。您可以使用offset具有负值的修饰符,但它会将Divider左侧移动。

您可以使用layout修改器应用水平偏移来增加宽度。

就像是:

LazyColumn(
    Modifier.background(Yellow),
    contentPadding = PaddingValues(all = 16.dp),
    verticalArrangement = Arrangement.spacedBy(16.dp),
) {
    //...

    item {
        val sidePadding = (-8).dp

 
        Divider(modifier = Modifier
            .layout { measurable, constraints ->
                // Measure the composable adding the side padding*2 (left+right)
                val placeable =
                    measurable.measure(constraints.offset(horizontal = -sidePadding.roundToPx() * 2))

                //increase the width adding the side padding*2
                layout(
                    placeable.width + sidePadding.roundToPx() * 2,
                    placeable.height
                ) {
                    // Where the composable gets placed
                    placeable.place(+sidePadding.roundToPx(), 0)
                }

            }
        )          
        
    }
}
Run Code Online (Sandbox Code Playgroud)

Divider()在这里您可以找到带有without 修饰符和Dividerwith修饰符的输出layout

在此输入图像描述