常用方法
- android:max="100" //滑动条的最大值
- android:progress="60" //滑动条的当前值
- android:secondaryProgress="70" //二级滑动条的进度
- android:thumb = "@mipmap/sb_icon" //滑块的drawable
代理方法SeekBar.OnSeekBarChangeListener
- onProgressChanged:进度发生改变时会触发
- onStartTrackingTouch:按住SeekBar时会触发
- onStopTrackingTouch:放开SeekBar时触发
案例实战
SeekBarActivity
public class SeekBarActivity extends BaseActivity {
private TextView processLab;
private SeekBar seekBarView;
private int currentProgress = 20; // 默认当前进度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mContext = SeekBarActivity.this;
initView();
}
public void initView() {
processLab = findViewById(R.id.processLab);
processLab.setText(currentProgress + " / 100% ");
seekBarView = findViewById(R.id.seekBarView);
seekBarView.setProgress(currentProgress);
seekBarView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
processLab.setText(progress + " / 100% ");
currentProgress = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
}
});
}
}
布局Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/processLab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="20/100%" />
<SeekBar
android:id="@+id/seekBarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:max="100"
android:maxHeight="5.0dp"
android:minHeight="5.0dp"
android:progress="20"
android:progressDrawable="@drawable/seekbar_bg"
android:thumb="@drawable/seekbar_thumb"
android:thumbOffset="0dp" />
</LinearLayout>
</LinearLayout>
条形栏Bar的Drawable:
seekbar_bg
,负责条形栏的背景颜色。
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="20dp" />
<solid android:color="#757575" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#FFFFFFFF" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="20dp" />
<solid android:color="#F66109" />
</shape>
</clip>
</item>
</layer-list>
条形栏Bar的Thumb:
seekbar_thumb
,负责拖动滑杆的样式。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 默认状态 -->
<item android:drawable="@drawable/seekbar_pole" />
<!-- 触摸状态 -->
<item android:drawable="@drawable/seekbar_pole" android:state_pressed="true" />
<!-- 未触摸状态 -->
<item android:drawable="@drawable/seekbar_pole" android:state_pressed="false" />
</selector>
网友评论