Android progress bar with padding

前端 未结 4 1640
情深已故
情深已故 2020-12-06 03:19

I want to make my progress bar looks something like that:

\"enter

I dont want

相关标签:
4条回答
  • 2020-12-06 03:33
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/background">
            <shape>
                <solid android:color="#222" />
                <corners android:radius="10dp" />
            </shape>
        </item>
        <item android:id="@android:id/progress">
            <clip>
                <layer-list>
                    <item>
                        <color android:color="#00000000" />
                    </item>
                    <item
                        android:left="5dp"
                        android:top="5dp"
                        android:right="5dp"
                        android:bottom="5dp">
                        <shape>
                            <solid android:color="#00FF00" />
                            <corners android:radius="10dp" />
                        </shape>
                    </item>
                </layer-list>
            </clip>
        </item>
    </layer-list>
    

    see my blog

    0 讨论(0)
  • 2020-12-06 03:37

    Unfortunately I didn't find solution for my problem. The only way I could make round corners was to use rounded 9-pitch image

    There is a good tutorial to do that: http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/

    0 讨论(0)
  • 2020-12-06 03:51

    A simple workaround is to wrap progress bar into layout which has rounded shape and same background color as progress bar.

    Sample progress bar drawable (drawable/progress_bar.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@android:id/background">
            <shape>
                <corners android:radius="50dip" />
                <solid android:color="@color/white"/>
                <padding android:bottom="3dip" android:left="3dip" android:right="3dip" android:top="3dip" />
            </shape>
        </item>
    
        <item android:id="@android:id/progress">
            <bitmap android:src="@drawable/test_progress_bar_progress" android:tileMode="repeat"/>
        </item>
    </layer-list>
    

    Progress bar style:

    <resources>
        <style name="ProgressBarStyle" parent="@android:style/Widget.ProgressBar.Horizontal">
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">10dip</item>
            <item name="android:max">100</item>
            <item name="android:progressDrawable">@drawable/progress_bar</item>
        </style>
    </resources>
    

    Layout shape (drawable/rounded_shape.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <padding android:top="2dip" android:left="2dip" android:right="2dip" android:bottom="2dip"/>
        <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
        <solid android:color="@color/white"/>
    </shape>
    

    Activity layout:

    <!-- ... -->
          <LinearLayout                     
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:background="@drawable/rounded_shape"
                      android:layout_marginLeft="30dip"
                      android:layout_marginRight="30dip">
                <ProgressBar
                        android:id="@+id/testSummaryProgressBar"
                        android:progress="10"
                        style="@style/ProgressBarStyle"/>
            </LinearLayout>
    <!-- ... -->
    

    Effect:

    enter image description here

    To increase padding of progress bar you have to increase padding in rounded_shape file.

    0 讨论(0)
  • 2020-12-06 03:52

    Yes it's possible to have exactly that without using image like @Nykakin has suggested. To have the padding and the rounded corner for the progress is to use list-layer for the item progress.

    <item android:id="@android:id/progress">
    <clip>
      <layer-list>
           <-- transparent background  -->
       <item>
           <color android:color="#00000000" />
       </item>
       <-- padding -->
       <item  
          android:left="2dp"
          android:top="2dp"
          android:right="2dp"
          android:bottom="2dp">
        <shape>
            <corners android:radius="50dp" />
    
            <gradient
                android:angle="270"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
         </shape>
       </item>
      </layer-list>
    </clip>
    

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