Android ProgressBar 进度条
Android ProgressBar(进度条) 可以用来显示一个操作的进度,一般用于比较耗时的地方,比如下载进度条,比如加载等待
ProgressBar
ProgressBar 继承自 View ,所以可以使用 XML 来创建
<ProgressBar android:id="@+id/indeterminateBar" android:layout_width="wrap_content" android:layout_height="wrap_content" />
我们写一个范例来演示 ProgressBar
- 
创建一个 空的 Android 项目 cn.twle.android.ProgressBar
- 
修改 activity_main.xml文件添加几个ProgressBar<?xml version="1.0" encoding="utf-8" ?> <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:padding="8dp" android:orientation="vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminate = "false" android:progress="25" android:max="100" /> <ProgressBar android:layout_width="match_parent" style="@android:style/Widget.ProgressBar.Inverse" android:layout_height="wrap_content" /> <ProgressBar android:layout_marginTop="16dp" android:layout_width="match_parent" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_height="wrap_content" android:indeterminate = "false" android:progress="25" android:max="100" /> </LinearLayout> 
运行结果如下
ProgressBar 外观
ProgressBar 有两种模式,确定性 和 不确定性的 ,有两种外观模式,条形 和 圆形,所以总共有四种,这涉及到两个属性
| 属性 | 说明 | 
|---|---|
| android:indeterminate | 是否显示不确定模式,默认为 true | 
| style | ProgressBar的外观样式 | 
android:style 属性有以下几个值可以选择
| 属性 | 说明 | 
|---|---|
| Widget.ProgressBar.Horizontal | |
| Widget.ProgressBar.Small | |
| Widget.ProgressBar.Large | |
| Widget.ProgressBar.Inverse | |
| Widget.ProgressBar.Small.Inverse | |
| Widget.ProgressBar.Large.Inverse | 
我们写一个 demo 来演示下 ProgressBar 的所有外观
- 
复用上面的 demo 
- 
修改 activity_main.xml文件添加几个ProgressBar<?xml version="1.0" encoding="utf-8" ?> <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:padding="8dp" android:orientation="vertical" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small" /> <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" style="@android:style/Widget.ProgressBar.Inverse" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small.Inverse" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large.Inverse" /> </LinearLayout> 
运行结果如下
是不是很难看,我看到的时候觉得真的是丑到家了...
ProgressBar 属性
| 属性 | 说明 | 
|---|---|
| android:max | 进度条的最大值 | 
| android:progress | 进度条已完成进度值 | 
| android:progressDrawable | 设置轨道对应的Drawable对象 | 
| android:indeterminate | 如果设置成true,则进度条不精确显示进度 | 
| android:indeterminateDrawable | 设置不显示进度的进度条的Drawable对象 | 
| android:indeterminateDuration | 设置不精确显示进度的持续时间 | 
| android:secondaryProgress | 二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过 progress 属性进行设置 | 
ProgressBar 方法
| 方法 | 说明 | 
|---|---|
| getMax() | 返回这个进度条的范围的上限 | 
| getProgress() | 返回进度 | 
| getSecondaryProgress() | 返回次要进度 | 
| incrementProgressBy(int diff) | 指定增加的进度 | 
| isIndeterminate() | 指示进度条是否在不确定模式下 | 
| setIndeterminate(boolean indeterminate) | 设置不确定模式下 | 
参考文档
官方 API 文档: ProgressBar