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

Android中如何去除Row组件内的多余垂直空白?

解决Jetpack Compose Row嵌套OutlinedButton的多余垂直空白问题

这种情况的核心原因是OutlinedButton本身自带默认的垂直内边距和最小高度,Row只是自适应了Button的完整尺寸,看起来像是Row有多余空白,实际是Button的默认样式导致的。以下是两种直接有效的解决方式:

方法一:自定义Button的内容内边距

通过调整contentPadding参数,缩小Button自带的垂直内边距,Row会自动适配Button的新高度:

Row(verticalAlignment = Alignment.Top) {
    OutlinedButton(
        onClick = {},
        // 按需调整内边距,默认值为PaddingValues(horizontal = 16.dp, vertical = 8.dp)
        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 4.dp)
    ) {
        Text("Button")
    }
}

方法二:让Button使用最小固有高度

通过Modifier.height(IntrinsicSize.Min)让Button高度完全贴合文本内容,配合手动给文本加内边距避免按钮过于紧凑:

Row {
    OutlinedButton(
        onClick = {},
        modifier = Modifier.height(IntrinsicSize.Min),
        contentPadding = PaddingValues(0.dp)
    ) {
        Text("Button", modifier = Modifier.padding(horizontal = 16.dp, vertical = 6.dp))
    }
}

如果需要完全自定义按钮样式,也可以复制OutlinedButton的默认样式,修改其中的minHeight和contentPadding参数,实现更极致的适配。

内容的提问来源于stack exchange,提问作者Surendra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:52:05