How to set color to secondary drawable in progress bar

前端 未结 2 1268
滥情空心
滥情空心 2021-01-03 16:32

I have this code

 pb = (ProgressBar) findViewById(R.id.progressBar1);
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDr         


        
相关标签:
2条回答
  • 2021-01-03 16:48

    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>
    

    0 讨论(0)
  • 2021-01-03 17:10

    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

    0 讨论(0)
提交回复
热议问题