Android自定義橫向的ProgressBar

廢話少說,先看一下效果吧,顏色有點醜,不過基本功能都有提到,實際使用中修改一下就ok。android

其實很簡單,上代碼!佈局

在res下建立drawable文件夾,新建文件drawable/progressbar_color.xml
code

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 背景  gradient是漸變,corners定義的是圓角 -->
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="10dp" />

            <solid android:color="#ffffff" />
        </shape>
    </item>
    <!-- 第二條進度條顏色 -->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <gradient
                    android:angle="90.0"
                    android:centerColor="#ac6079"
                    android:centerY="0.45"
                    android:endColor="#6c213a"
                    android:startColor="#e71a5e" />
            </shape>
        </clip>
    </item>
    <!-- 進度條 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="10dip" />

                <solid android:color="#FF8080" />
            </shape>
        </clip>
    </item>

</layer-list>

而後在佈局中引用就能夠了。xml

activity_main.xmlip

<ProgressBar 
        android:id="@+id/my_progress"
        android:layout_width="match_parent"
        android:layout_height="12dp"
        android:max="100"
        android:progress="40"
        android:secondaryProgress="70"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/progressbar_color"/>

運行以後就是上面的效果啦。
utf-8

相關文章
相關標籤/搜索