You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jetpack Compose的Row/Column/Card中添加左侧边框

为动态高度的Column添加左侧垂直边框的解决方案

方法1:用Modifier.drawBehind自定义绘制

不需要额外组件,直接在Column背后绘制适配其高度的左侧边框,完全跟着内容高度变化:

Column(
    modifier = Modifier
        .padding(start = 34.dp)
        .drawBehind {
            val borderWidth = 2.dp.toPx()
            val startOffset = 34.dp.toPx() - borderWidth // 对应Column的start padding,调整边框位置
            drawLine(
                color = Color.Red,
                start = Offset(startOffset, 0f),
                end = Offset(startOffset, size.height),
                strokeWidth = borderWidth
            )
        }
) {
    // 这里放你的动态内容
    Text("测试内容1")
    Text("测试内容2")
    // ... 更多内容
}

方法2:用Row包裹Divider和Column

通过Row让Divider高度自动匹配Column的实际高度,不用固定height:

Row(
    verticalAlignment = Alignment.Top,
    modifier = Modifier.padding(start = 34.dp)
) {
    Divider(
        color = Color.Red,
        modifier = Modifier
            .fillMaxHeight() // 高度自动跟随Row(即Column的高度)
            .width(2.dp)
            .padding(end = 34.dp)
    )
    Column {
        // 原Column的动态内容
        Text("测试内容1")
        Text("测试内容2")
        // ... 更多内容
    }
}

内容的提问来源于stack exchange,提问作者J.K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 18:45:37