我有一个自定义控件,需要在其中创建多个具有特定样式的ProgressBar
,并将它们添加到LinearLayout
容器中,并保持一定的间距(创建后唯一的修改是设置进度,并在第一个项目上设置0个开始页边距,在最后一个项目上设置0个结束页边距。
我正在使用以下代码创建ProgressBar
实例:
if (container.childCount != progressSteps) {
container.removeAllViews()
repeat(progressSteps) {
container.addView(generateProgressBar())
}
container.children.first().apply {
layoutParams = (layoutParams as LinearLayout.LayoutParams).apply {
marginStart = 0
}
}
container.children.last().apply {
layoutParams = (layoutParams as LinearLayout.LayoutParams).apply {
marginEnd = 0
}
}
}
这是应该更新每个进度状态的位:
(0 until container.childCount).forEach { index ->
val child = container[index]
val shouldTint = index < currentProgress
(child as ProgressBar).progress = if (shouldTint) 100 else 0
}
说明:
container
是我的LinearLayout
,意在包含ProgressBar
实例progressSteps
和currentProgress
是我的View类上的Int
变量属性generateProgressBar()
创建具有某种自定义样式(基本上使用ProgressBar
样式,覆盖android.R.attr.progressBarStyleHorizontal
和某些布局参数的progressDrawable
实例)我最终得到的是最后一个ProgressBar
被着色,但是根据LayoutInspector转储,前两个应该是着色的。但是它们保持灰色。