
1.在res/drawable目录下新增一个xml风格文件,seekbar_define_style.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | xmlns:android = "http://schemas.android.com/apk/res/android" > |
06 | android:id = "@android:id/background" |
07 | android:drawable = "@drawable/hou" /> |
10 | android:id = "@android:id/progress" |
11 | android:drawable = "@drawable/qian" /> |
13 | android:id = "@android:id/secondaryProgress" |
14 | android:drawable = "@drawable/qian" /> |
2.在res/drawable下定义个seekbar_thumb.xml文件
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < selector xmlns:android = "http://schemas.android.com/apk/res/android" > |
06 | android:state_focused = "true" |
07 | android:state_pressed = "true" |
08 | android:drawable = "@drawable/ic_launcher" /> |
11 | android:state_focused = "false" |
12 | android:state_pressed = "false" |
13 | android:drawable = "@drawable/orbino_icon_pack_006" /> |
16 | android:state_focused = "true" |
17 | android:state_pressed = "false" |
18 | android:drawable = "@drawable/ios" /> |
21 | android:state_focused = "true" |
22 | android:drawable = "@drawable/ios" /> |
3.在res/layut下定义布局资源文件seekbar_define.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < ScrollView xmlns:android = "<a href=" http://schemas.android.com/apk/res/android" rel = "nofollow" >http://schemas.android.com/apk/res/android</ a >" |
03 | android:layout_width="fill_parent" |
04 | android:layout_height="fill_parent" |
07 | android:layout_width = "fill_parent" |
08 | android:layout_height = "fill_parent" |
09 | android:orientation = "vertical" |
12 | android:id = "@+id/seekbar_tetview_one" |
13 | android:layout_width = "wrap_content" |
14 | android:layout_height = "wrap_content" |
15 | android:text = "SeekBar自定义" |
18 | android:id = "@+id/seekbar_tetview_two" |
19 | android:layout_width = "wrap_content" |
20 | android:layout_height = "wrap_content" |
21 | android:text = "SeekBar拖动时信息提示" |
24 | android:layout_width = "321px" |
25 | android:layout_height = "wrap_content" |
26 | android:layout_centerInParent = "true" |
27 | android:maxHeight = "20px" |
28 | android:minHeight = "20px" |
29 | android:paddingLeft = "18px" |
30 | android:paddingRight = "18px" |
32 | android:progressDrawable = "@drawable/seekbar_define_style" |
33 | android:thumb = "@drawable/seekbar_thumb" |
34 | android:id = "@+id/seekBar" /> |
4.定义java文件通过 引用布局文件:
03 | import android.R.integer; |
04 | import android.app.Activity; |
05 | import android.os.Bundle; |
06 | import android.os.Handler; |
07 | import android.os.Message; |
08 | import android.widget.SeekBar; |
09 | import android.widget.SeekBar.OnSeekBarChangeListener; |
10 | import android.widget.TextView; |
12 | public class SeekBarDemo_DefineDemo extends Activity { |
13 | private SeekBar seekBar; |
14 | private TextView textView_one, textView_two; |
17 | protected void onCreate(Bundle savedInstanceState) { |
18 | super .onCreate(savedInstanceState); |
19 | setContentView(R.layout.seekbar_define); |
21 | seekBar = (SeekBar) findViewById(R.id.seekBar); |
23 | textView_one = (TextView) findViewById(R.id.seekbar_tetview_one); |
25 | textView_two = (TextView) findViewById(R.id.seekbar_tetview_two); |
27 | seekBar.setOnSeekBarChangeListener(seekbarChangeListener); |
31 | private OnSeekBarChangeListener seekbarChangeListener = new OnSeekBarChangeListener() { |
35 | public void onStopTrackingTouch(SeekBar seekBar) { |
37 | textView_two.setText( "停止拖动了!" ); |
43 | public void onStartTrackingTouch(SeekBar seekBar) { |
45 | textView_two.setText( "进度开始改变" ); |
50 | public void onProgressChanged(SeekBar seekBar, int progress, |
52 | textView_two.setText( "正在进行拖动操作,还没有停下来一直再拖动" ); |
53 | Message message = new Message(); |
55 | Bundle bundle = new Bundle(); |
57 | float pro = seekBar.getProgress(); |
59 | float num = seekBar.getMax(); |
61 | float result = (pro / num) * 100 ; |
62 | bundle.putFloat( "key" , result); |
64 | message.setData(bundle); |
68 | handler.sendMessage(message); |
76 | private Handler handler = new Handler() { |
78 | public void handleMessage(Message msg) { |
79 | textView_one.setText( "当前拖动位置占 : " |
80 | + msg.getData().getFloat( "key" ) + "/100" ); |
最后执行效果:



二:使用颜色显示,和尚面是一样的,只有我们定义颜色资源来替代图片资源文件seekbar_define_color_style.xml:如下:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < layer-list xmlns:android = "http://schemas.android.com/apk/res/android" > |
04 | < item android:id = "@android:id/background" |
05 | android:paddingTop = "3px" |
06 | android:paddingBottom = "3px" > |
08 | < corners android:radius = "10dip" /> |
10 | android:startColor = "#ffffffff" |
11 | android:centerColor = "#ff000000" |
12 | android:endColor = "#ff808A87" |
13 | android:centerY = "0.45" |
18 | < item android:id = "@android:id/progress" |
19 | android:paddingTop = "3px" |
20 | android:paddingBottom = "3px" > |
23 | < corners android:radius = "10dip" /> |
25 | android:startColor = "#ffffffff" |
26 | android:centerColor = "#ffFFFF00" |
27 | android:endColor = "#ffAABD00" |
28 | android:centerY = "0.45" |
之后再SeekBar标签使用如下属性进行引入:其他保持不变
1 | android:progressDrawable="@drawable/seekbar_define_color_style" |
执行效果:

由于SeekBar的属性thumb引入了自定义的seekbar_thumb.xml文件,拖动图标是我们自定义的图片:除去这个属性
1 | android:thumb="@drawable/seekbar_thumb" |
就回复系统默认状态效果最后效果如下:

我们可以通过颜色值再次休息seekbar_thumb.xml文件,使拖动按钮设置成自定义颜色: