如何获取Android的本机进度条不确定的可绘制对象?

问题描述

我想知道是否有可能检索Android本机ProgressBar不确定可绘制对象,以便可以在其他视图上使用。

查看ProgressBar的源代码

  1. 属性位于com.android.internal.R.attr.progressBarStyle,我相信它与android.R.attr.progressBarStyle相同(根据this answer)。
  2. 我正在寻找的可绘制对象的资源ID是R.styleable.ProgressBar_indeterminateDrawable

但是,我无法访问该可设置样式的属性本身。

关于如何实现这种目标的任何想法吗?

解决方法

这就是我在我的应用中所做的。 IIRC,此代码由 Jake Wharton 在改造站点上提供,用于在文件下载时获取进度条。

fun getProgressBarIndeterminate(): Drawable? {
    val attrs = intArrayOf(android.R.attr.indeterminateDrawable)
    val attrsIndeterminateDrawableIndex = 0
    val a = App.getContext().obtainStyledAttributes(android.R.style.Widget_ProgressBar,attrs)
    return try {
        a.getDrawable(attrsIndeterminateDrawableIndex)
    } catch (e: Exception) {
        return null
    }finally {
        a.recycle()
    }
}