Android UI详解之SeekBar、ProgressBar和RatingBar
***ProgressBar本身是一个进度条,他派生了两个子类SeekBar和RatingBar
Android中支持几种风格的进度条,通过Style属性指定风格
style="@android:style/Widget.ProgressBar.Large" 大环进度条
style="@android:style/Widget.ProgressBar.Small"
style="@android:style/Widget.ProgressBar.Small.Inverse"
style="@android:style/Widget.ProgressBar.Horizontal" 水平进度条
style="@android:style/Widget.ProgressBar.Inverse" 普通大小环形进度条
style="@android:style/Widget.ProgressBar.Large.Inverse" 大环进度条
他的常用xml属性
android:max 设置进度条最大值
android:progress 设置进度条的已完成值
android:progressDrawable 设置进度条的轨道
android:indeterminate 如果属性设为True,设置进度条为不精确显示
android:indeterminateDrawable 设置绘制不显示进度的进度条
android:indeterminateDuration 设置不精确显示进度的持续时间
操作进度的方法
setProgress(int) 设置进度条的完成百分比
第二个进度值的设置,调用setSecondaryProgress()方法
incrementProgressBy(int) 设置进度条的进度增加或减少,但参数为正数时进度增加,当参数为负数时进度减少
incrementSecondaryProgressBy() 设置第二进度条的进度增加或减少,但参数为正数时进度增加,当参数为负数时进度减少
一、ProgressBar进度条实现
<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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="小圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="中圆形进度条(即默认)" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大圆形进度条" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水平进度条" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:max="100"
android:progress="30"
android:secondaryProgress="60" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加进度" />
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少进度" />
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener {
private Button btn1, btn2;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button01);
btn2 = (Button) findViewById(R.id.button02);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
setProgressBarVisibility(true);
setProgressBarIndeterminate(true);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button01:
progressBar.setProgress((int) (progressBar.getProgress() * 1.2));
progressBar.setSecondaryProgress((int) (progressBar
.getSecondaryProgress() * 1.1));
break;
case R.id.button02:
progressBar.incrementProgressBy(-2);
progressBar.incrementSecondaryProgressBy(-1);
break;
}
}
}
二、seekbar拖动条实现
<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" >
<TextView
android:id="@+id/textview01"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview02"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekbar01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
<SeekBar
android:id="@+id/seekbar02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30"
android:secondaryProgress="60" />
</LinearLayout>
java代码
public class MainActivity extends Activity implements OnSeekBarChangeListener {
private TextView tv1, tv2;
private SeekBar seekBar1, seekBar2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.textview01);
tv2 = (TextView) findViewById(R.id.textview02);
seekBar1 = (SeekBar) findViewById(R.id.seekbar01);
seekBar2 = (SeekBar) findViewById(R.id.seekbar02);
seekBar1.setOnSeekBarChangeListener(this);
seekBar2.setOnSeekBarChangeListener(this);
}
// 滑动滑竿触发的事件
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (seekBar.getId() == R.id.seekbar01) {
tv2.setText("seekBar1的当前位置:" + progress);
} else {
tv2.setText("seekBar2的当前位置:" + progress);
}
}
// 表示从哪里开始拖动
public void onStartTrackingTouch(SeekBar seekBar) {
if (seekBar.getId() == R.id.seekbar01) {
tv1.setText("seekBar1开始拖动");
} else {
tv1.setText("seekBar2开始拖动");
}
}
// 表示从哪里结束拖动
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getId() == R.id.seekbar01) {
tv1.setText("seekBar1结束拖动");
} else {
tv1.setText("seekBar2结束拖动");
}
}
}
<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" >
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
java,代码
public class MainActivity extends Activity implements OnRatingBarChangeListener {
private TextView tv;
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textview);
ratingBar = (RatingBar) findViewById(R.id.ratingbar);
ratingBar.setMax(100);
ratingBar.setProgress(30);
tv.setText("Progress:" + ratingBar.getProgress() + " Rating:"
+ ratingBar.getRating());
ratingBar.setOnRatingBarChangeListener(this);
}
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
tv.setText("Progress:" + ratingBar.getProgress() + " Rating:"
+ ratingBar.getRating());
}
}