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中加入這句:
效果圖:
好醜!!!!!!!!!