在我们的开发过程中,时而使用到SeekBar这个组件,而系统默认的样式有时候满足不了我们的需求,那么我们就需要自己定义SeekBar了。
图:
好了,闲言少叙,我们直接上干货:
1.在我们的布局中定义SeekBar,设置id,最大进度,当前进度,progressDrawable(滑动的背景),thumb(滑块的背景),thumbOffset(偏移量)。
<SeekBar
android:id="@+id/seek_forget_pas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:progressDrawable="@drawable/my_seekbar_style"
android:thumb="@mipmap/progressdraw"
android:thumbOffset="0dp" />
2.下面是 progressDrawable(@drawable/my_seekbar_style) 的内容:
PS:定义 background,就是我们整体的背景为图中蓝色的带字的背景。
secondaryProgress 就像我们在听歌曲时的缓冲条
progress是我们已经划过去的地方的显示状态,就是白色滑块左边的全蓝色
注意***:在我们设置这个style的时候,我遇到了关于适配的问题,除了在每个尺寸文件夹下都加入一张图片外,还需要在设置这个style的时候, background 必须有,而secondaryProgress"和progress,至少有一个,不然尺寸会有问题。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@mipmap/run1" />
<item android:id="@android:id/secondaryProgress">
<scale android:drawable="@mipmap/run4" android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
<scale android:drawable="@mipmap/run4" android:scaleWidth="100%" />
</item>
</layer-list>
3.thumb就是一张图片。
到此,我们的设置都完成了,就可以定制自己的SeekBar了。