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

如何让LazyRow中最后一个Item的高度与其他Item保持一致?

解决LazyRow中自定义Item与其他Item高度一致的问题

下面给你两种可行的实现方式:

方法一:直接复用固定尺寸修饰符

既然其他Item已经用了固定的size(100.dp),给按钮所在的Column也加上同样的尺寸约束,再通过内部布局让Button居中显示,就能保证高度一致:

LazyRow() {
    items(data) {
        Column(
            modifier = Modifier.size(100.dp).background(Color.DarkGray)
                .padding(16.dp)
        ) {
            Text(
                text = it,
                color = Color.White
            )
        }
    }
    item {
        Column(
            modifier = Modifier.size(100.dp).background(Color.DarkGray),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(
                onClick = {}
            ) {
                Text(text = "Submit")
            }
        }
    }
}

这个方法简单直接,适合已经明确固定Item高度的场景。

方法二:利用固有尺寸实现动态适配

如果不想硬写固定高度,或者后续Item高度可能变化,可以用Compose的固有尺寸特性,让LazyRow的高度由子项中最高的那个决定,再让按钮Column填充整个行高:

LazyRow(
    modifier = Modifier.height(IntrinsicSize.Min)
) {
    items(data) {
        Column(
            modifier = Modifier.size(100.dp).background(Color.DarkGray)
                .padding(16.dp)
        ) {
            Text(
                text = it,
                color = Color.White
            )
        }
    }
    item {
        Column(
            modifier = Modifier.fillMaxHeight().background(Color.DarkGray),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Button(
                onClick = {}
            ) {
                Text(text = "Submit")
            }
        }
    }
}

这里IntrinsicSize.Min会让LazyRow取所有子项中最小的固有高度(也就是你设置的100dp),按钮的Column用fillMaxHeight()就能自动匹配这个高度,不用硬写数值,扩展性更好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 12:23:03