//SeekBar的定义
<?xml version="1.0" encoding="utf-8"?> <SeekBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/seekbar_toumingdu" android:thumb="@drawable/seekbar_thumb_bigoval" android:progressDrawable="@drawable/seekbar_progress" android:paddingLeft="15dp" android:paddingRight="15dp" android:thumbOffset="15dp" android:maxHeight="5dp" android:layout_width="280dp" android:layout_height="wrap_content" > </SeekBar>
thumb定义上面的滑块的drawable实现,progressDrawable定义下面长条的实现。
thumb使用shapDrawable定义为一个圆形
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:height="30dp" android:width="30dp"/> <solid android:color="#8003a9f4"/> </shape>
progressDrawable定义:
<?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> <gradient android:angle="270" android:centerColor="#ffffff" android:centerY="0.45" android:endColor="#ffffff" android:startColor="#d4d4d4" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#03a9f4"/> </shape> </clip> </item> </layer-list>子项目item的id指定相应的drawable实现,使用item的drawable属性直接指定好像有点问题。
有background,progress和secondrayProgress三个,选择其中需要的的即可。
几个需要注意的问题:
(1)自定义的滑块滑动到边缘时要是中心处于边缘位置,那么会有一部分滑块超出长条边缘,这时要指定paddingleft和paddingright,不然不能显示超出部分的滑块。
(2)滑块滑到边缘时,还有可能中心位置并不是恰好处于边缘,这时候要指定thumbOffset属性,长度为滑块宽的一半,那样就能显示好了。
(3)长条宽度是通过maxHeight指定,使用其它方法指定宽度不行,不影响滑块的。
想要实现更炫的滑块,使用渐变的shap,layerDrawable等,不知道为什么用到手机这些效果都不行了,不知道有没有人懂。