动态创建的进度条-错误的子顺序?

时间:2019-05-21 15:10:13

标签: android

我有一个自定义控件,需要在其中创建多个具有特定样式的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实例
  • progressStepscurrentProgress是我的View类上的Int变量属性
  • generateProgressBar()创建具有某种自定义样式(基本上使用ProgressBar样式,覆盖android.R.attr.progressBarStyleHorizontal和某些布局参数的progressDrawable实例)

我最终得到的是最后一个ProgressBar被着色,但是根据LayoutInspector转储,前两个应该是着色的。但是它们保持灰色。

0 个答案:

没有答案