android - 如何根据进度百分比设置垂直进度条颜色?
问题描述
有什么解决方案如何根据进度百分比放置进度条颜色吗?例如,如果我的progressBar 0-5% 它是红色的,如果它是5-50% 它是黄色的。如果它是 50-100%,它是绿色的。
解决方案
当您设置进度时,您还可以设置它的颜色。对于 API 21+;
val progress = (0..100).random()
progressBar.apply {
this.progress = progress
progressTintList = ColorStateList.valueOf(
when {
progress <= 5 -> Color.RED
progress in 6..50 -> Color.YELLOW
else -> Color.GREEN
}
)
}
根据最大进度 14 和当前进度 7 进行编辑
val maxProgress = 14
val currentProgress = 7
val fixedProgress = (100.0 / maxProgress * currentProgress).roundToInt()
progressBar.apply {
max = maxProgress
progress = currentProgress
progressTintList = ColorStateList.valueOf(
when {
fixedProgress <= 5 -> Color.RED
fixedProgress in 6..50 -> Color.YELLOW
else -> Color.GREEN
}
)
}
推荐阅读
- ios - 在引入“tuist”进行快速项目管理时,“tuist edit”关键字不起作用
- html - HTML:当垂直滚动仅在父元素上时冻结水平滚动条位置
- python - 将不同的列值堆叠到熊猫数据框中的一列中
- swiftui - 在 NavigationView 之外设置 NavigationView 选择
- pid - 为 microbit 制作 PID 脚本
- python - 创建嵌入矩阵时如何确定 num_words 变量?
- bash - 如何在 GitLab 中检索给定组名的对应组 ID
- python - 您可以将属于单个项目的不同 django 应用程序容器化到不同的 docker 容器中吗?
- environment-variables - “仅支持绝对 URL”当获取 webhooks Nextjs
- apache-zookeeper - 如何在 Apache Zookeeper ver3.6.2 中禁用纯文本端口