SeekBar介绍
在我们使用音乐播放器或者是视频播放器时,下面都会有一个进度条,拖动进度条即可改变 音乐的进度和视频播放的进度,那么在安卓里面也有相应的工具类,它就是SeekBar。
使用Seekbar主要看三个属性:
属性名 | 备注 |
---|---|
android:max=”100” | 滑动条最大值,这里设置为100 |
android:progress=”30” | 滑动条初始值,这里设置为30 |
android:secondaryProgress=”50” | 滑动条缓冲值(常见于音频视频播放),这里设置为50 |
制作可拖动的进度条
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.turo.seekbartest.MainActivity">
<SeekBar
android:max="100"
android:progress="30"
android:secondaryProgress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar" />
<TextView
android:id="@+id/tv1"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
实现代码:
package com.turo.seekbartest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView1,textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.tv1);
textView2 = (TextView) findViewById(R.id.tv2);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int max = seekBar.getMax();
String s = i + "/" + max;
textView1.setText("正在拖动");
textView2.setText("当前数值" + s);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
textView1.setText("开始拖动");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
textView1.setText("停止拖动");
}
});
}
}
效果图:
Android系统自带Seekbar样式
在布局文件的SeekBar中加入这句:
style="@android:style/Widget.SeekBar"
效果图:
好丑!!!!!!!!!