Android UI之ProgressBar(进度条)

来源:互联网 发布:手机解压缩软件 编辑:程序博客网 时间:2024/06/06 04:32

进度条是一个很实用的组件,一般用来显示用户某个耗时操作的进度百分比,首先来看一下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.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="进度条演示" />  
  11.   
  12.     <ProgressBar   
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:max="1000"  
  16.         android:progress="100"  
  17.         android:id="@+id/progressbar1"  
  18.         />  
  19.       
  20.     <ProgressBar   
  21.         style="@android:style/Widget.ProgressBar.Inverse"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:max="100"  
  25.         android:progress="20"  
  26.         />  
  27.         <ProgressBar   
  28.         style="@android:style/Widget.ProgressBar.Large"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:max="100"  
  32.         android:progress="20"  
  33.         />  
  34.      <ProgressBar   
  35.         style="@android:style/Widget.ProgressBar.Large.Inverse"  
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content"  
  38.         android:max="100"  
  39.         android:progress="20"  
  40.         />  
  41.     <ProgressBar   
  42.         style="@android:style/Widget.ProgressBar.Small"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content"  
  45.         android:max="100"  
  46.         android:progress="20"  
  47.         />  
  48.     <ProgressBar   
  49.         style="@android:style/Widget.ProgressBar.Small.Inverse"  
  50.         android:layout_width="wrap_content"  
  51.         android:layout_height="wrap_content"  
  52.         android:max="100"  
  53.         android:progress="20"  
  54.         />      
  55.     <ProgressBar   
  56.         style="@android:style/Widget.ProgressBar.Horizontal"  
  57.         android:layout_marginTop="30dp"  
  58.         android:layout_width="fill_parent"  
  59.         android:layout_height="wrap_content"  
  60.         android:max="1000"  
  61.         android:progress="500"  
  62.         android:secondaryProgress="300"  
  63.         android:id="@+id/progressbar2"  
  64.         />  
  65. </LinearLayout>  
在模拟器上的效果:


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

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

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

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

android::secondaryProgress 设置第二进度条


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

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

0 0