2.09SeekBar(拖动条)-UI组件-Android
SeekBar(拖动条)-UI组件-Android
常用属性
属性名 | 说明 |
---|---|
android:max="100" | 滑动条的最大值 |
android:progress="60" | 滑动条的当前值 |
android:secondaryProgress="70" | 二级滑动条的进度 |
android:thumb = "@mipmap/sb_icon" | 滑块的drawable |
SeekBar.OnSeekBarChangeListener
的方法
方法 | 说明 |
---|---|
onProgressChanged: | 进度发生改变时会触发 |
onStartTrackingTouch: | 按住SeekBar时会触发 |
onStopTrackingTouch: | 放开SeekBar时触发 |
案例
<?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"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="@+id/sb_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="30sp" />
<SeekBar
android:id="@+id/sb_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="5dp"
android:minHeight="5dp"
android:progressDrawable="@drawable/sb_bar"
android:thumb="@drawable/sb_thumb" />
</LinearLayout>
package com.ttit.helloworld;
import android.content.Context;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class SeekBarActivity extends AppCompatActivity {
private SeekBar sb_normal, sb_custom;
private TextView txt_cur;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar_layout);
mContext = this;
sb_normal = (SeekBar) findViewById(R.id.sb_normal);
sb_custom = (SeekBar) findViewById(R.id.sb_custom);
txt_cur = (TextView) findViewById(R.id.txt_cur);
//拖动监听事件
sb_normal.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txt_cur.setText("当前进度值:" + progress + " / 100 ");
}
@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();
}
});
sb_custom.setSecondaryProgress(20);//二级进度条,缓冲值
}
}
定制滑块:sb_bar.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"><!-- 未选中的滑块背景色--黄色-->
<shape>
<solid android:color="#FFFFD042" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress"><!-- 二级背景色-->
<clip>
<shape>
<solid android:color="#999999" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress"><!--一级选中背景色 -->
<clip>
<shape>
<solid android:color="#FF96E85D" />
</shape>
</clip>
</item>
</layer-list>
定制滑块样式:sb_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/seekbar_thumb_pressed" android:state_pressed="true" /><!-- 滑块按下时,滑块样式图形-->
<item android:drawable="@mipmap/seekbar_thumb_normal" android:state_pressed="false" /><!-- 滑块松开时,滑块样式图形-->
</selector>