天天看点

Android UI之ProgressBar(进度条)

进度条是一个很实用的组件,一般用来显示用户某个耗时操作的进度百分比,首先来看一下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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.     <TextView  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="进度条演示" />  
  10.     <ProgressBar   
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:max="1000"  
  14.         android:progress="100"  
  15.         android:id="@+id/progressbar1"  
  16.         />  
  17.     <ProgressBar   
  18.         style="@android:style/Widget.ProgressBar.Inverse"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:max="100"  
  22.         android:progress="20"  
  23.         />  
  24.         <ProgressBar   
  25.         style="@android:style/Widget.ProgressBar.Large"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:max="100"  
  29.         android:progress="20"  
  30.         />  
  31.      <ProgressBar   
  32.         style="@android:style/Widget.ProgressBar.Large.Inverse"  
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content"  
  35.         android:max="100"  
  36.         android:progress="20"  
  37.         />  
  38.     <ProgressBar   
  39.         style="@android:style/Widget.ProgressBar.Small"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:max="100"  
  43.         android:progress="20"  
  44.         />  
  45.     <ProgressBar   
  46.         style="@android:style/Widget.ProgressBar.Small.Inverse"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:max="100"  
  50.         android:progress="20"  
  51.         />      
  52.     <ProgressBar   
  53.         style="@android:style/Widget.ProgressBar.Horizontal"  
  54.         android:layout_marginTop="30dp"  
  55.         android:layout_width="fill_parent"  
  56.         android:layout_height="wrap_content"  
  57.         android:max="1000"  
  58.         android:progress="500"  
  59.         android:secondaryProgress="300"  
  60.         android:id="@+id/progressbar2"  
  61.         />  
  62. </LinearLayout>  

在模拟器上的效果:

Android UI之ProgressBar(进度条)

从上到下的六个进度条第一个没有声明样式,他默认的为和第二个一样是普通大小的进度条,第三个第四个分别是大进度条,第五个第六个是小进度条,最后一个是水平进度条

除了样式ProgressBar的几个常用属性:

android::max  设置进度条的最大值(最后一个水平进度条的设置为1000)

android::progress  设置当前的进度(最后一个水平进度条设置当前进度为500)

android::secondaryProgress 设置第二进度条

模拟一个进度条的进度变动:

[html]  view plain  copy

  1. package cn.class3g.activity;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.util.Log;  
  6. import android.widget.ProgressBar;  
  7. public class ProgressBarDemo extends Activity{  
  8.     ProgressBar progressbar = null;  
  9.     static int i = 0;  
  10.     int progressbarMax = 0;  
  11.     Handler handler = new Handler();  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.progressbar_layout);  
  15.         findViews();  
  16.     }  
  17.     private void findViews() {  
  18.         progressbar = (ProgressBar) this.findViewById(R.id.progressbar2);  
  19.         progressbar.setMax(1000);  
  20.         progressbarMax = progressbar.getMax();  
  21.         //利用线程控制进度条的进度  
  22.         new Thread(new Runnable(){  
  23.             public void run(){  
  24.                 while(i< progressbarMax){  
  25.                     //doWord()模拟一个任务的进度  
  26.                     i=doWork();  
  27.                     handler.post(new Runnable(){  
  28.                         public void run(){  
  29.                             progressbar.setProgress(i);  
  30.                         }  
  31.                     });  
  32.                     try {  
  33.                         Thread.sleep(50);  
  34.                     } catch (InterruptedException e) {  
  35.                         // TODO Auto-generated catch block  
  36.                         e.printStackTrace();  
  37.                     }  
  38.                 }  
  39.             }  
  40.         }).start();  
  41.     }  
  42.     public int doWork(){  
  43.         Log.d("TAG", String.valueOf(i));  
  44.         return ++i;  
  45.     }  
  46. }  

效果:

Android UI之ProgressBar(进度条)