【问题标题】:Custom ProgressBar in kotlinkotlin 中的自定义进度条
【发布时间】:2017-06-24 13:51:20
【问题描述】:

我正在尝试制作自定义 ProgressBar,就像我以前做自定义布局的方式一样。这次我遇到了问题。

我可以通过这个 xml 获得想要的外观:

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminateOnly="false"
    android:progressDrawable="@drawable/progress_bar" />

然而,最终目标是在自定义类中进行一些自定义,以便 xml 缩小为:

<se.my.viktklubb.app.progressbar.HorizontalProgressBar
    android:id="@+id/planProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

这是自定义类:

class HorizontalProgressBar : ProgressBar {

    constructor(context: Context) : super(context) {
        initialSetup()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initialSetup()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initialSetup()
    }

    fun initialSetup() {
        max = 100
        isIndeterminate = false
        progressDrawable = context.getDrawable(R.drawable.progress_bar)
    }
}

第二个构造函数被触发,但该栏没有样式。它实际上显示为不确定的旋转进度,并且这些设置最终都没有应用 - 感觉这些设置稍后会被其他东西覆盖。

这里有什么问题?

免责声明: 这是一个非常简单的例子。我非常清楚我可以选择样式或简单的 xml 实现,但我只是使用这个简单的案例来演示这个问题。

【问题讨论】:

    标签: android progress-bar android-custom-view kotlin


    【解决方案1】:

    根据article的第5条,你可能需要修改initialSetup:

    fun initialSetup() {
        max = 100
        isIndeterminate = false
        progressDrawable = context.getDrawable(R.drawable.progress_bar)
        invalidate()
        requestLayout()
    }
    

    如果它没有改变任何东西,请稍后尝试调用 initialSetup,例如在活动/片段的 onResume 上。如果这个可行,问题可能与android组件的初始化/生命周期的复杂性有关。

    【讨论】:

    • 链接已失效。 :(
    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 2016-05-06
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多