当一个应用在后台执行时,前台界面就不会有什么信息,这时用户根本不知道程序是否在执行、执行进度如何、应用程序是否遇到错误终止等,这时需要使用进度条来提示用户后台程序执行的进度。Android系统提供了两大类进度条样式,长形进度条(progress-BarStyleHorizontal)
和圆形进度条(progressBarStyleLarge)。进度条用处很多,比如,应用程序装载资源和网络连接时,可以提示用户稍等,这一类进度条只是代表应用程序中某一部分的执行情况,而整个应用程序执行情况呢,则可以通过应用程序标题栏来显示一个进度条,这就需要先对窗口的显示风格进行设置"requestWindowFeature(Window.FEATURE_PROGRESS)"。
我们看看这个示例程序的 运行效果
先上布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/ProgressBar02"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:visibility="gone"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
</LinearLayout>
ProgressBar01 为长形进度条,Progress02为圆形进度条,标题栏进度条没有在这里声明。"style="表示设置进度条的风格。下面通过setMax方法设置进度条的最大值,setProgress 方法设置进度条的当前值,setVisibility 方法设置进度条的可见性。
android 2.0 APILEVEL 5
源码图片 见附件