天天看點

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(進度條)

繼續閱讀