SeekBar的简单使用
XML:添加控件
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:max="1000"
android:minHeight="20dp"
android:maxHeight="20dp"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:splitTrack="false"
android:progressDrawable="@drawable/seekbar_style"
android:thumb="@drawable/select_seekbar" />
android:max="1000" 设置seekbar的最大值,需要自己按控制seekbar进度的比例
设置SeekBar的最小高度,和最大高度;当自定义progressDrawable无法居中显示时,只需要设置这两个参数为同一值,就可以解决问题
android:minHeight="20dp" android:maxHeight="20dp"
设置SeekBar的开始、结束的Padding,默认SeekBar的Padding不为0
android:paddingStart="0dp" android:paddingEnd="0dp"
当设置SeekBar的ProgressDrawable与进度条出现空隙时,需要将android:splitTrack设置为false android:splitTrack="false"
简单自定义设置SeekBar的progressDrawable
android:progressDrawable="@drawable/seekbar_style
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
//根据id设置资源类型(seekBar背景)
<item android:id="@android:id/background">
<shape android:shape="line">
<stroke android:color="@color/color_808080"
android:width="0.5dp" />
</shape>
</item>
//已加载的进度drawable
<item android:id="@android:id/progress">
<clip>
<shape android:shape="line">
<stroke android:color="#ffffffff"
android:width="1dp" />
</shape>
</clip>
</item>
//还可以设置二级progress,一般作为缓冲进度使用
//android:id="@android:id/secondaryProgress"
</layer-list>
设置SeekBar的thumb可以根据不用的状态设置多种drawable android:thumb="@drawable/select_seekbar"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/drawable_no_focused"/>
<item
android:state_pressed="true"
android:drawable="@drawable/drawable_thnumb"/>
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/drawable_thnumb"/>
</selector>