进度条是一个很实用的组件,一般用来显示用户某个耗时操作的进度百分比,首先来看一下Android支持的几种风格的进度条:
style="@android:style/Widget.ProgressBar.Inverse" 普通大小进度条
style="@android:style/Widget.ProgressBar.Large" 大进度条
style="@android:style/Widget.ProgressBar.Large.Inverse" 大进度条
style="@android:style/Widget.ProgressBar.Small" 小进度条
style="@android:style/Widget.ProgressBar.Small.Inverse" 小进度条
style="@android:style/Widget.ProgressBar.Horizontal"水平进度条
在样式文件中分别添加这六个不同样式的进度条,看看在模拟器上的效果
[html] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="进度条演示" />
- <ProgressBar
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="1000"
- android:progress="100"
- android:id="@+id/progressbar1"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Large"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Large.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Small"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Small.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_marginTop="30dp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:max="1000"
- android:progress="500"
- android:secondaryProgress="300"
- android:id="@+id/progressbar2"
- />
- </LinearLayout>
在模拟器上的效果:
从上到下的六个进度条第一个没有声明样式,他默认的为和第二个一样是普通大小的进度条,第三个第四个分别是大进度条,第五个第六个是小进度条,最后一个是水平进度条
除了样式ProgressBar的几个常用属性:
android::max 设置进度条的最大值(最后一个水平进度条的设置为1000)
android::progress 设置当前的进度(最后一个水平进度条设置当前进度为500)
android::secondaryProgress 设置第二进度条
模拟一个进度条的进度变动:
[html] view plain copy
- package cn.class3g.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.widget.ProgressBar;
- public class ProgressBarDemo extends Activity{
- ProgressBar progressbar = null;
- static int i = 0;
- int progressbarMax = 0;
- Handler handler = new Handler();
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.progressbar_layout);
- findViews();
- }
- private void findViews() {
- progressbar = (ProgressBar) this.findViewById(R.id.progressbar2);
- progressbar.setMax(1000);
- progressbarMax = progressbar.getMax();
- //利用线程控制进度条的进度
- new Thread(new Runnable(){
- public void run(){
- while(i< progressbarMax){
- //doWord()模拟一个任务的进度
- i=doWork();
- handler.post(new Runnable(){
- public void run(){
- progressbar.setProgress(i);
- }
- });
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- public int doWork(){
- Log.d("TAG", String.valueOf(i));
- return ++i;
- }
- }
效果: