我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?


当前回答

在修改默认进度条的外观时遇到了同样的问题。这里有一些更多的信息,希望能帮助到人们:)

The name of the xml file must only contain characters: a-z0-9_. (ie. no capitals!) To reference your "drawable" it is R.drawable.filename To override the default look, you use myProgressBar.setProgressDrawable(...), however you need can't just refer to your custom layout as R.drawable.filename, you need to retrieve it as a Drawable:Resources res = getResources(); myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename); You need to set style before setting progress/secondary progress/max (setting it afterwards for me resulted in an 'empty' progress bar)

其他回答

你可以尝试改变你的风格,主题,或使用android:indeterminateTint="@color/yourColor"任何你想要的地方,但只有一种方法来做,将工作在任何android SKD版本:

如果你的进度条不是不确定的,请使用:

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );

如果进度条不确定,请使用:

progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );

Android真是一团糟!

很抱歉,这不是答案,但是是什么驱使需求从代码中设置它呢? 如果定义正确。setprogressdrawable应该工作

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/progress_start"
                android:endColor="@color/progress_end"
                android:angle="270" 
            />
        </shape>
    </clip>
</item>

</layer-list>

对于我的不确定的进度条(转轮),我只是设置了一个颜色过滤器的可绘制。工作很好,只是一行。

将颜色设置为红色的示例:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

相信我,最简单的解决方法就是把这个粘贴到progressBar中:

android:indeterminateTint="@android:color/white"

还有一件小事,如果你继承了一个基础主题,主题解决方案也会起作用,所以对于应用紧凑,你的主题应该是:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>

然后在进度条主题中设置这个

<ProgressBar
    android:id="@+id/progressCircle_progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:theme="@style/AppTheme.Custom"
    android:indeterminate="true"/>