转载连接:seekBar自定义样式
1.xml文件:
<SeekBar
android:id="@+id/sb_run_watt"
android:layout_width="400dp"
android:layout_height="15dp"
android:max="100"
android:maxHeight="15dp"
android:minHeight="15dp"
android:progress="34"
android:thumbOffset="0dp"
android:progressDrawable="@drawable/seekbar_style"
android:thumb="@drawable/seekbar_thumb"/>
可以发现属性:thumbOffset为0,是为了确保thumb,滑到左右俩端时,不会滑出去;
并且layout_width值不等于progressDrawable的宽度;
2.seekbar_style.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" android:drawable="@drawable/seekbar_bg" /
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@android:color/transparent"/>
</item>
<item android:drawable="@drawable/seekbar_bg"/>
</selector>
</scale>
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%">
<selector>
<item android:state_enabled="false">
<color android:color="@android:color/transparent"/>
</item>
<item android:drawable="@drawable/seekbar_fg"/>
</selector>
</scale>
</item>
</layer-list>
主要注意:3个item的顺序,以及透明度100%,因为防止后面被挡住,当然如果用color表示progress,如下:
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="3dp"/>
<solid android:color="#a691ff02"/>
</shape>
</clip>
</item>
3.seekbar_thumb.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/seekbar_slider" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/seekbar_slider" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/seekbar_slider" />
<item android:state_focused="true" android:drawable="@drawable/seekbar_slider" />
</selector>
网友评论