当应用程序在进行一些耗时操作时(如网络下载,图片资源的加载等),应用程序会处于一个“等待”的状态,在这个时候如果不通知用户程序正在运行中,用户可能认为程序“未响应”而关闭程序,这样的用户体验显然非常糟糕。因此我们可以用一个进度条让用户知道当前程序的进度信息,提高用户体验。
Android提供的进度条控件一般以两种样式显示,一种是环形样式,一种是线性样式,可以通过“style”属性来设置样式。此外进度条有“确定”和“不确定”两种,前者可以精确的表示操作执行的进度;后者只能表示操作正在执行,可以通过“android:indeterminate”属性来设置。“确定”的进度条有两个进度,一个是主进度(可表示操作执行的重进度),一个是第二进度(可用来表示缓存进度,如在线看视频的进度条等)
XML常用属性:
android:indeterminate 是否设置为不精确模式
android:progressDrawable 用来设置外观样式
android:max 设置最大进度值
android:progress 设置默认第一进度值
android:secondaryProgress 设置默认第二进度值
style="?android:attr/progressBarStyleHorizontal" 设置进度条显示样式为线性
常用方法:
getMax():返回这个进度条的范围的上限
getProgress():返回进度
getSecondaryProgress():返回次要进度
incrementProgressBy(int diff):指定增加的进度
isIndeterminate():指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate):设置不确定模式下
setVisibility(int v):设置该进度条是否可视
onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件
效果预览:
1.布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/progress_bar"
android:id="@+id/progressBar" />
<TextView
android:id="@+id/textView"
android:text=