How to set color to secondary drawable in progress

2019-02-11 08:12发布

问题:

I have this code

 pb = (ProgressBar) findViewById(R.id.progressBar1);
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
    String MyColor = "#00FF00";
    pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    pb.setProgressDrawable(progress);
    pb.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));

The problem in this code is that for the progress drawable and for the secondary progress drawable I have the same color.

How to set the socondary progress color ?

回答1:

Specify a progressDrawable like in the example below:

This goes to a layout:

<ProgressBar
    android:id="@+id/gauge"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="4dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/section"
    android:progress="50" 
    android:progressDrawable="@drawable/progressbar"/>

This goes to drawable/progressbar.xml, and here you can specify background and colors for both progress bars.

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

<item android:id="@android:id/background">
    <shape>
        <gradient
                android:startColor="@color/basecolor"
                android:endColor="@color/basecolor"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <gradient
                    android:startColor="#808080"
                    android:endColor="#808080"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#ffcc33"
                android:endColor="#ffcc33"
                android:angle="270" />
        </shape>
    </clip>
</item>



回答2:

I have also faced the same issue. I add some extra points to the above answer. The seekbar or progressbar is drawn in three layers. First layer is background layer, above that secondary progress layer, above that primary progress.

you can set your custom color to progressbar like this

<item android:id="@android:id/background"
    android:drawable="@drawable/progress_bar_nor">       
</item>    
<item android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/progress_bar_nor">       
</item>
<item
    android:id="@android:id/progress"
    android:drawable="@drawable/progress_bar_sel"/>

or like Ridcully said in above answer

It happens that some android os versions do not use the background color of secondary progress. I have checked on android 4.1.2 samsung 8 inch tablet. It doesnot work. In that scenario use background to set your custom color for the secondary progress by setting background, secondary progress color