ProgressBar
有两种主要的形态:一个是圆形的,一个是长条形的,形如:
圆形的
ProgressBar
,通常用于未确定的何时结束的进度显示,它会一直显示动画。
长条形的
ProgressBar
,通常用于进度明确的进度显示。
1.
在布局文件
(main.xml)
中,增加界面元素声明如下:
<?
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"
>
<
ProgressBar
android:id
=
"@+id/progress_bar1"
style
=
"?android:attr/progressBarStyleSmall"
<!--
小圆
ProgressBar -->
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
ProgressBar
android:id
=
"@+id/progress_bar2"
<!--
缺省地,是中圆
ProgressBar -->
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
ProgressBar
android:id
=
"@+id/progress_bar3"
style
=
"?android:attr/progressBarStyleLarge"
<!--
大圆
ProgressBar -->
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
/>
<
ProgressBar
android:id
=
"@+id/progress_bar4"
style
=
"?android:attr/progressBarStyleHorizontal"
<!--
长条形
ProgressBar -->
android:layout_width
=
"fill_parent"
android:layout_height
=
"8sp"
<!--
指明高度为
8sp-->
android:max
=
"100"
<!--
最大值为
100 -->
/>
<
TextView
<!--
此
TextView
做分隔符用
-->
android:layout_width
=
"fill_parent"
android:layout_height
=
"6sp"
/>
<
ProgressBar
android:id
=
"@+id/progress_bar5"
style
=
"?android:attr/progressBarStyleHorizontal"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:max
=
"100"
/>
<
Button
<!--
用于触发进度条相关操作
–>
android:id
=
"@+id/button"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"
进度条测试
"
/>
</
LinearLayout
>
对应的界面结果显示如下:
2.
通过程序的方式,我们还可以把进度条放到应用的标题栏
(Title bar)
中。这个可以用程序来实现:
requestWindowFeature(Window.
FEATURE_INDETERMINATE_PROGRESS
);
//
在
Titlebar
中,放置圆形进度条
requestWindowFeature(Window.
FEATURE_PROGRESS
);
//
在
Titlebar
中,放置长条形进度条
setContentView(R.layout.
main
);
setProgressBarIndeterminateVisibility(
true
);
//
在
Titlebar
中,显示圆形进度条
setProgressBarVisibility(
true
);
//
在
Titlebar
中,显示长条形进度条
注意:
requestWindowFeature
方法的调用,必须在
setContentView
之前!
3.
Activity
所对应的代码: