ProgressBar也是一组重要的的组件,ProgressBar本身代表了进度条组件,它还派生了两个常用的组件:SeekBar和RatingBar。ProgressBar及其子类在用法上十分相似,只是显示界面有一定的区别。

ProgressBar及其子类的继承关系如图所示

 

  • 进度条(ProgressBar)的功能与用法

Android支持多种风格的进度条,通过style属性可以为ProgressBar指定风格。该属性可支持如下几个属性值。

  1. @android:style/Widget.ProgressBar.Horizontal:水平进度条。
  2. @android:style/Widget.ProgressBar.Inverse:普通大小的环形进度条。
  3. @android:style/Widget.ProgressBar.Large:大环形进度条。
  4. @android:style/Widget.ProgressBar.Laege.Inverse:大环形进度条。
  5. @android:style/Widget.ProgressBar.Laege.Small:小环形进度条。
  6. @android:style/Widget.ProgressBar.Laege.Small,Inverse:小环形进度条。

ProgressBar提供了如下方法来操作进度。

  1. setProgress(int):设置进度的完成百分比。
  2. incrementProgressBar(int):设置进度条的进度增加或减少。当参数为正数时进度增加;当参数为负数时进度减少。

下面的程序简单示范了进度条的用法。该程序的界面布局文件只是定义了几个简单的进度条,并指定style属性为@android:style/Widget.ProgressBar.Horizontal,即水平进度条。界面布局文件如下。

复制代码
<?xml version="1.0" encoding="utf-8"?><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="match_parent"         android:layout_height="wrap_content"         android:text="任务完成的进度"/>    <!--定义一个水平进度条-->    <ProgressBar         android:id="@+id/bar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:max="100"         style="@android:style/Widget.ProgressBar.Horizontal"/>    <!--定义一个水平进度条,并改变轨道外观-->    <ProgressBar         android:id="@+id/bar2"         android:layout_width="match_parent"         android:layout_height="wrap_content"