Android修改seekbar颜色(Material Desi
2018-05-10 本文已影响57人
进击的鱼儿
布局
<android.support.v7.widget.AppCompatSeekBar
android:id="@+id/vertical_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seekbar_style"
android:thumb="@drawable/seekbar_thumb"
/>
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:gravity="center_vertical|fill_horizontal">
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<size android:height="2dp"/>
<solid android:color="#80b6b6b6"/>
</shape>
</item>
<item
android:id="@android:id/secondaryProgress"
android:gravity="center_vertical|fill_horizontal">
<clip>
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<size android:height="2dp"/>
<solid android:color="@color/orange_disenable"/>
</shape>
</clip>
</item>
<item
android:id="@android:id/progress"
android:gravity="center_vertical|fill_horizontal">
<clip>
<shape android:shape="rectangle">
<corners android:radius="5dp"/>
<size android:height="2dp"/>
<solid android:color="@color/orange"/>
</shape>
</clip>
</item>
</layer-list>
这里特别注意的就是一定不能忘了加android:gravity="center_vertical|fill_horizontal"
,不然效果就会背景就会变得很粗,设置的<size android:height="2dp">
无效。
seekbar_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="@color/white"/>
<size
android:width="12dp"
android:height="12dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="@color/white"/>
<size
android:width="12dp"
android:height="12dp"/>
</shape>
</item>
<item>
<shape
android:shape="oval"
android:useLevel="false">
<solid android:color="@color/orange"/>
<size
android:width="12dp"
android:height="12dp"/>
</shape>
</item>
</selector>