進度條也是UI界面中一種非常實用的組件,通常用於向用戶顯示某個耗時操作完成的的百分比。進度條可以動態的顯示進度,因此避免長時間的執行某個耗時的操作,讓用戶感覺程序失去了響應,從而更好的提高用戶界面的友好性。
Android支持幾種風格的進度條,通過style屬性可以為ProgressBar指定風格。該屬性克支持如下幾個屬性值:
- @android:style/Widget.ProgressBar.Horizontal:水平進度條。
- @android:style/Widget.ProgressBar.Inverse:普通大小的環形進度條。
- @android:style/Widget.RpogressBar.Large:大環形進度條。
- @android:style/Widget.ProgressBar.Large.Inverse:大環形進度條。
- @android:style/Wdget.ProgressBar.Small:小環形進度條。
- @android:style/Widget.ProgressBar.Small.Inverse:小環形進度條。
其中android:progressDrawable用於指定進度條的軌道的繪制形式,該屬性可指定為一個LayerDrawable對象(該對象可通過在XML文件中用<layer-list>元素進行配置)的引用。
ProgressBar提供如下方法來操作進度。
- setProgress(int):設置進度的完成百分比。
- incrementProgressBy(int):設置進度條的進度增加或減少。當參數為正數時進度增加;當參數為負數時進度減少。
下面的程序簡單示范了進度條的用法,該程序的界面布局文件只是定義了幾個簡單的進度條,並指定style屬性為@android:style/Widget.ProgressBar.Horizontal,即水平進度條。界面布局文件如下。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 定義一個大環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large" /> <!-- 定義一個中等大小的環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 定義一個小環形進度條 --> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small" /> </LinearLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="任務完成的進度" /> <!-- 定義一個水平進度條 --> <ProgressBar android:id="@+id/bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" style="@android:style/Widget.ProgressBar.Horizontal" /> <!-- 定義一個水平進度條,並改變軌道外觀 --> <ProgressBar android:id="@+id/bar2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/my_bar" style="@android:style/Widget.ProgressBar.Horizontal" /> </LinearLayout>
上面的布局文件中先定義了三個環形進度條,這種葷腥進度條無法顯示進度,它只是顯示一個不斷旋轉的圖片。布局文件的后面定義的兩個進度條的最大值為100,第一個進度條的樣式為水平進度條;第二個進度條的外觀被定義為@drawble/my_bar,因此還需要在drawable-mdpi中定義如下文件。、
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 定義軌道的背景 --> <item android:id="@android:id/background" android:drawable="@drawable/no"/> <!-- 定義軌道上已完成部分的樣式 --> <item android:id="@android:id/progress" android:drawable="@drawable/ok"/> </layer-list>
下面主程序用一個填充數組的任務模擬了耗時操作,並以進度條來標識任務的完成百分比,主程序如下。
package org.crazyit.helloworld; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.widget.ProgressBar; public class ProgessBarTest extends Activity { //該程序模擬填充長度為100的數組 private int[] data=new int[100]; int hasData=0; //記錄ProgressBar的完成進度 int status=0; ProgressBar bar,bar2; //創建一個負責更新進度的Handler @SuppressLint("HandlerLeak") Handler mHandler=new Handler() { public void handleMessage(Message msg) { //表明消息是由該程序發送的 if(msg.what==0x111) { bar.setProgress(status); bar2.setProgress(status); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progess_bar_test); bar=(ProgressBar)findViewById(R.id.bar); bar2=(ProgressBar)findViewById(R.id.bar2); //啟動線程來執行任務 new Thread() { public void run() { while(status<100) { //獲取耗時操作的完成百分比 status=doWork(); //發送消息 mHandler.sendEmptyMessage(0x111); } } }.start(); } //模擬一個耗時的操作 public int doWork() { //為數組元素賦值 data[hasData++]=(int)(Math.random()*100); try { Thread.sleep(100); } catch(InterruptedException e) { e.printStackTrace(); } return hasData; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.progess_bar_test, menu); return true; } }
上面的程序中粗體字代碼用於修改進度條的完成進度。運行上面的程序將看到如下效果: