天天看點

Android 利用shape自定義進度條樣式

轉載請注明出處。付小華的移動開發微網誌:http://blog.csdn.net/klxh2009

很少寫部落格,今天來一個。先看效果圖:

Android 利用shape自定義進度條樣式

項目檔案結構:

1、drawable

2、stytles

3、layout

一、先看layout:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B4E7BA"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/myprogressbar"
        style="@style/style_myprogressbar"
        android:layout_width="fill_parent"
        android:layout_height="10dp"
        android:layout_margin="20dp"
        android:background="@drawable/shape_pb_bg"
        android:max="100"
        android:progress="40" />

</LinearLayout></span>
           

二、再看stytles:

<style name="style_myprogressbar" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:maxHeight">50dip</item>
        <item name="android:minHeight">10dip</item>
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:progressDrawable">@drawable/shape_pb_bg</item>
    </style>
           

三、drawable下的shape_pb_bg:

<?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="5dip" />

            <gradient
                android:angle="270"
                android:centerY="0.75"
                android:endColor="#FFFFFF"
                android:startColor="#FFFFFF" />
        </shape>
    </item>
    <!-- 進度 -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="#1D00FF"
                    android:startColor="#1D00FF" />
            </shape>
        </clip>
    </item>

</layer-list>
           

OK,有這些就夠了,謝謝。

繼續閱讀