How to change progress bar's progress color in

2018-12-31 09:04发布

I'm using an horizontal progress bar in my Android application, and I want to change its progress color (which is Yellow by default). How can I do it using code (not XML)?

30条回答
素衣白纱
2楼-- · 2018-12-31 09:49

There's probably one thing that hasn't been referred to in this answer:

If your theme is inheriting from Theme.AppCompat, ProgressBar will assume the color you defined as "colorAccent" in your theme.

So, using..

<item name="colorAccent">@color/custom_color</item>

..will tint the color of the ProgressBar automagically to the @color/custom_color .

查看更多
弹指情弦暗扣
3楼-- · 2018-12-31 09:49
android:progressTint="#ffffff" 
查看更多
皆成旧梦
4楼-- · 2018-12-31 09:50

Hit the same problem while working on modifying the look/feel of the default progress bar. Here is some more info that will hopefully help people :)

  • 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)
查看更多
孤独寂梦人
5楼-- · 2018-12-31 09:50

Apply this custom style to the progress bar.

<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:indeterminateDrawable">@drawable/progress</item>
        <item name="android:duration">40</item>
        <item name="android:animationCache">true</item>
        <item name="android:drawingCacheQuality">low</item>
        <item name="android:persistentDrawingCache">animation</item>
    </style>

@drawable/progress.xml -

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white"
    android:pivotX="50%"
    android:pivotY="50%" />

Use this type of image for progress bar. enter image description here

For better result you can use multiple progress images. And please don't hesitate using images, because Android Platform itself use images for progressbar. The code is extracted from sdk :)

查看更多
步步皆殇っ
6楼-- · 2018-12-31 09:52

All API

if use all API just create the theme in style

style.xml

<resources>

    //...

    <style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/blue</item>
    </style>

</resources>

and use in progress

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

API level 21 and higher

if used in API level 21 and higher just use this code:

<ProgressBar
   //...
   android:indeterminate="true"
   android:indeterminateTintMode="src_atop"
   android:indeterminateTint="@color/secondary"/>
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 09:52

As per muhammad-adil suggested for SDK ver 21 and above

android:indeterminateTint="@color/orange"

in XML Works for me, is easy enough.

查看更多
登录 后发表回答