進度條是一個很實用的元件,一般用來顯示使用者某個耗時操作的進度百分比,首先來看一下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;
- }
- }
效果: