更改Seekbar样式
Android4.0 默认的seekbar样式如图1所示,可以根据个人喜好对SeekBar的外观进行修改,图2为我更改后的SeekBar。
图1 默认的seekBar
图2 更改后的seekBar
更改步骤:
一、新建一个seekBar的style
打开res\values\style.xml文件,加入如下代码:
1 <style name="SeekBar"> 2 <item name="android:indeterminateOnly">false</item> 3 <item name="android:progressDrawable">@drawable/progress_horizontal</item> 4 <item name="android:minHeight">3dip</item> 5 <item name="android:maxHeight">3dip</item> 6 <item name="android:thumb">@drawable/seek_thumb</item> 7 <item name="android:thumbOffset">16dip</item> 8 </style>
style其实是控件的一组属性的集合,不使用style,直接在控件的示例xml中依次指定上述的几个属性也可以达到同样的效果。
注释:
1 <style name="SeekBar">
添加名为“SeekBar”的style。
2 <item name="android:indeterminateOnly">false</item>
progressBar有indeterminate模式,此模式下,progressBar的长度和目前所处的位置不确定,progressBar显示循环的动画,没有具体的进度,如图3就为一个indeterminate的prigressBar。
图3 indeterminate模式的progressBar
3 <item name="android:progressDrawable">@drawable/progress_horizontal</item>
这个标签指定的是进度条的drawable文件,此处指定res\drawable\progress_horizontal.xml,该文件在步骤二中新建。
4 <item name="android:minHeight">3dip</item> 5 <item name="android:maxHeight">3dip</item>
设置seekbar的最小和最大高度。
6 <item name="android:thumb">@drawable/seek_thumb</item>
指定seekbar的thumb即小圆按钮的drawable文件,此处指定res\drawable\seek_thumb.xml,在步骤三中新建该文件。
7 <item name="android:thumbOffset">16dip</item>
thumbOffset指的是thumb图片可以横向偏移进度条的距离。此值设置为0时,progress = 0的情况下,thumb图片的最左端与progressbar_background的最左端对齐。适当调整此值可以使得thumb的中心在起点和终点能与进度条两端完全重合。纵向上,thumb的中心默认与progressbar_background的中心重合,不需调整。
二、新建progress_horizontal.xml文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <item android:id="@android:id/background"> 5 <nine-patch 6 android:src="@drawable/progressbar_background" 7 /> 8 </item> 9 10 <item android:id="@android:id/secondaryProgress"> 11 <clip> 12 <shape> 13 <corners android:radius="5dip" /> 14 <gradient 15 android:startColor="#80ffd300" 16 android:centerColor="#80ffb600" 17 android:centerY="0.75" 18 android:endColor="#a0ffcb00" 19 android:angle="270" 20 /> 21 </shape> 22 </clip> 23 </item> 24 25 <item android:id="@android:id/progress"> 26 <clip> 27 <nine-patch 28 android:src="@drawable/progressbar_progress" 29 /> 30 </clip> 31 </item> 32 33 </layer-list>
layer list

浙公网安备 33010602011771号