Material3 CircularProgressIndicator无stroke参数,无法设置圆角帽求助
解决CircularProgressIndicator圆角样式问题
这种情况大多是因为你用的Compose版本太低——CircularProgressIndicator的stroke参数(支持配置StrokeCap)是在Compose Material 1.2.0及以上版本才新增的,低版本里这个组件确实没开放该参数,内部硬编码为Square。
给你几个可行的解决办法:
升级Compose版本:在Module级的
build.gradle里把Compose相关依赖版本升到1.2.0或更高,示例配置:implementation "androidx.compose.material:material:1.6.0" // 记得同步compose-ui、compose-compiler等其他相关依赖的版本升级后就能直接通过
stroke参数设置圆角:CircularProgressIndicator( stroke = Stroke(width = 8.dp, cap = StrokeCap.Round) )自定义圆角进度条(暂时不能升级版本的话):用
Canvas自己实现一个带圆角的圆形进度条,代码示例:@Composable fun RoundedCircularProgressIndicator( progress: Float, strokeWidth: Dp = 8.dp, color: Color = MaterialTheme.colors.primary, backgroundColor: Color = color.copy(alpha = 0.3f) ) { val strokeWidthPx = strokeWidth.toPx() Canvas(modifier = Modifier.size(48.dp)) { // 绘制背景圆环 drawCircle( color = backgroundColor, style = Stroke(width = strokeWidthPx, cap = StrokeCap.Round) ) // 绘制进度圆弧 drawArc( color = color, startAngle = -90f, sweepAngle = progress * 360f, useCenter = false, style = Stroke(width = strokeWidthPx, cap = StrokeCap.Round) ) } }使用时直接调用:
RoundedCircularProgressIndicator(progress = 0.7f)
另外也可以检查下导包语句,确保你用的是androidx.compose.material.CircularProgressIndicator,别误导入了第三方UI库的同名组件。
内容的提问来源于stack exchange,提问作者Jozef Forgáč
相关产品推荐
相关产品推荐

