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

如何在不增加Button高度的情况下添加CircularProgressIndicator

问题描述

在Jetpack Compose中,我希望在Button内同时使用CircularProgressIndicator和Text组件。仅包含Text时,按钮显示正常:

Button(
    onClick = { }
) {
    Row() {
        Text("Some action")
    }
}

仅含Text的按钮

但添加CircularProgressIndicator后,按钮高度明显增加:

Button(
    onClick = {  }
) {
    Row() {
        CircularProgressIndicator(color = Color.White)
        Text("Some action")
    }
}

添加CircularProgressIndicator后的按钮

请问如何在Button中添加CircularProgressIndicator的同时,保持按钮初始高度?

解决方案

CircularProgressIndicator默认尺寸会超出Button的内边距范围,导致按钮被撑开。可以通过以下几种方式解决:

  • 缩小指示器尺寸:直接用modifier.size()指定适配Button高度的小尺寸,同时让Row垂直居中对齐,提升美观度:

    Button(
        onClick = { }
    ) {
        Row(verticalAlignment = Alignment.CenterVertically, gap = 8.dp) {
            CircularProgressIndicator(
                color = Color.White,
                modifier = Modifier.size(16.dp)
            )
            Text("Some action")
        }
    }
    
  • 限制指示器布局大小:通过wrapContentSize()让指示器在Button的可用空间内自适应,配合垂直居中避免偏移:

    Button(
        onClick = { }
    ) {
        Row(verticalAlignment = Alignment.CenterVertically, gap = 8.dp) {
            CircularProgressIndicator(
                color = Color.White,
                modifier = Modifier.wrapContentSize(align = Alignment.Center)
            )
            Text("Some action")
        }
    }
    
  • 调整Button内边距:自定义Button的contentPadding,抵消指示器带来的额外高度:

    Button(
        onClick = { },
        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
    ) {
        Row(verticalAlignment = Alignment.CenterVertically, gap = 8.dp) {
            CircularProgressIndicator(color = Color.White)
            Text("Some action")
        }
    }
    

其中第一种方法最直观,既能保持Button初始高度,又能让组件布局更协调。

内容的提问来源于stack exchange,提问作者me.at.coding

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 18:22:17