一. 简介
1 2 3 4
| android.view.View ↳android.widget.ProgressBar ↳android.widget.AbsSeekBar ↳android.widget.SeekBar
|
SeekBar继承于ProgressBar,那么ProgressBar有的属性,我们SeekBar也有。SeekBar多了一个thumb属性,用来设置滑块的图标。
二. 属性
1 2 3 4 5 6 7 8 9
| android:max="" android:progress=""
android:secondaryProgress="" android:progressDrawable="" android:thumb=""
|
三. 自定义progressDrawable
1 2
| android:progressDrawable="@drawable/progressbar_bg"
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?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="@color/colorProgress" />
<item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="50dp"/> <gradient android:endColor="#D2D2B0" android:startColor="#D2D2B0" /> </shape> </clip> </item>
<item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="50dp"/> <gradient android:endColor="#fff000" android:startColor="#fff000" /> </shape> </clip> </item>
</layer-list>
|
四. 监听滑动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| SeekBar seekBar = findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { }
@Override public void onStartTrackingTouch(SeekBar seekBar) { }
@Override public void onStopTrackingTouch(SeekBar seekBar) { } });
|
五. demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center">
<SeekBar style="@style/Widget.AppCompat.SeekBar" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="40dp" android:indeterminate="false" android:max="100" android:progress="80" android:secondaryProgress="100" />
<SeekBar android:layout_width="match_parent" android:layout_height="40dp" android:progressDrawable="@drawable/seekbar_bg" android:max="100" android:progress="80" android:secondaryProgress="100" android:thumb="@drawable/seek_bar_point" android:layout_marginTop="40dp" />
<SeekBar android:layout_width="match_parent" android:layout_height="40dp" android:progressDrawable="@drawable/seekbar_bg" android:max="100" android:progress="80" android:secondaryProgress="100" android:thumb="@drawable/seek_bar_point" android:background="@null" android:layout_marginTop="40dp" />
<SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="40dp" android:progressDrawable="@drawable/seekbar_bg" android:max="100" android:progress="80" android:secondaryProgress="100" android:thumb="@drawable/seek_bar_point" android:splitTrack="false" android:background="@null" android:layout_marginTop="40dp" />
<TextView android:id="@+id/show_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="80" android:textAlignment="center" android:textStyle="bold" android:layout_marginTop="40dp" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
final TextView textView = findViewById(R.id.show_text);
SeekBar seekBar = findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { textView.setText(String.valueOf(progress)); }
@Override public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override public void onStopTrackingTouch(SeekBar seekBar) {
} }); } }
|
运行效果:

参考文章
Android之ProgressBar(进度条)和SeekBar(拖动条)
seekbar thumb 透明效果出现父布局背景颜色的解决方法
解决SeekBar拖动过程中thumb周围产生的圆形阴影/白色圆圈