Android 自定义SeekBar背景样式

自定义Android系统控件的背景样式大同小异,现在以自定义SeekBar背景样式走下流程:

 1.SeekBar的进度条样式,custom_seekbar_progress.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>
            <solid android:color="#9e9e9e"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#e4017f"/>
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#9e9e9e"/>
            </shape>
        </clip>
    </item>

</layer-list>

这里是以自定义的颜色作为进度条背景,如果要使用图片资源,那就这样:

 <item
            android:id="@android:id/background"
            android:drawable="@drawable/seekbar_progess">
 </item>
 2.SeekBar的滑块样式custom_seekbar_thumb.xml:

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

    <item android:drawable="@drawable/seekbar_thumb_normal"
          android:state_focused="true" android:state_pressed="false"/>

    <item android:drawable="@drawable/seekbar_thumb_pressed"
          android:state_focused="true" android:state_pressed="true"/>

    <item android:drawable="@drawable/seekbar_thumb_pressed"
          android:state_focused="false"
          android:state_pressed="true"/>

    <item android:drawable="@drawable/seekbar_thumb_normal"/>

</selector>

分别定义按下状态与松开状态下的图片.

最后在SeekBar中使用我们自定义的属性:

<SeekBar
        android:maxHeight="3dpt"
        android:minHeight="3dp"
        android:progressDrawable="@drawable/custom_seekbar_progress"
        android:thumb="@drawable/custom_seekbar_thumb"
        ....../>

maxHeigh,minHeigh 用来指定进度条的高度.



你可能感兴趣的:(android)