How to set color to secondary drawable in progress bar

十年热恋 提交于 2019-11-30 16:17:23

问题


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



来源:https://stackoverflow.com/questions/12244994/how-to-set-color-to-secondary-drawable-in-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!