改变不确定水平进度条的背景颜色

6

我看到一些关于处理ProgressBar颜色的主题,但没有一个能回答我的疑问。

我正在使用水平ProgressBar Indeterminate类型。我希望它有一个透明的背景,同时有一个有色的progressBar,但找不到方法来实现。

对于普通的ProgressBar(非Indeterminate),我可以通过调用progressBar.getProgressDrawable()来获取LayerDrawable。然后使用findDrawableByLayerId(android.R.id.background),我可以只着色背景。

但是使用progressBar.getIndeterminateDrawable()会返回一个GradientDrawable,所以我无法按照相同的过程进行操作。

是否可能从GradientDrawable中获取所有API级别的颜色?因为如果可以,我可以获取背景颜色并改变它。

是否有任何解决方案?

3个回答

2

使用如下的颜色过滤器:

progressBar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);

1

progressBar.setProgressDrawable(R.drawable.progress_bar);

drawable/progress_bar.xml

<layer-list   xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="2dip" />
        <gradient android:startColor="#43ffffff" android:centerColor="#43ffffff" android:centerY="0.75" android:endColor="#43ffffff" android:angle="270" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="2dip" />
            <gradient android:startColor="#fff" android:endColor="#fff" android:angle="270" />
        </shape>
    </clip>
</item> </layer-list>

您可以在progress_bar.xml drawable中更改颜色。


0

步骤1:将SDK资源中的drawable/progress_indeterminate_horizontal.xml复制到您的项目中。

步骤2:将SDK资源中的drawable-hdpi/progressbar_indeterminate*复制到您的项目中。

步骤3:将SDK资源中的drawable-mdpi/progressbar_indeterminate*复制到您的项目中。

步骤4:修改PNG文件以符合您的要求。

步骤5:修改progress_indeterminate_horizontal.xml,使其指向您的PNG文件(而不是标准文件)。

步骤6:使用@drawable/progress_indeterminate_horizontal作为indeterminateDrawable值。

来源

否则

progressBar.getIndeterminateDrawable()
    .setColorFilter(progressBar.getContext().getResources().getColor(<colorId>),
        PorterDuff.Mode.SRC_IN);

这会更改我项目中所有不确定的进度条,对吗?如果是这样,是否有任何解决方法可以将其应用于项目中的单个进度条? - AndroidDev
@AndroidDev,(1st.)适用于所有人,(2nd.)不适用于特定的一个。 - W4R10CK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接