什么是ProgressBar
ProgressBar是用于提示用戶進(jìn)行等待的UI控件,.
基礎(chǔ)樣例
1.loading圖
效果圖

代碼
- 布局文件代碼
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- activity代碼
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//隱藏進(jìn)度條
progressBar.visibility = View.GONE
//顯示進(jìn)度條
progressBar.visibility = View.VISIBLE
}
}
2.橫向進(jìn)度條
效果圖

代碼
- 布局文件代碼
<ProgressBar
android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="20" />
- activity代碼
//橫向進(jìn)度條:設(shè)置進(jìn)度
horizontalProgressBar.progress = 50
3.橫向loading圖
效果圖

代碼
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
基礎(chǔ)樣例完整源代碼
https://gitee.com/cxyzy1/ProgressBarDemo
常用屬性說明
| 屬性名 | 用途 |
|---|---|
| android:layout_width | 設(shè)置控件寬度,可設(shè)置為:match_parent(和父控件一樣),wrap_content(按照內(nèi)容自動(dòng)伸縮),設(shè)置固定值(如200dp) |
| android:layout_height | 設(shè)置控件高度,可設(shè)置為:match_parent(和父控件一樣),wrap_content(按照內(nèi)容自動(dòng)伸縮),設(shè)置固定值(如200dp) |
| android:gravity | 控件內(nèi)對(duì)齊方式 |
| android:background | 設(shè)置背景,可以是色值(如#FF0000)或圖片等 |
| android:visibility | 可選值: visible(顯示), invisible(隱藏,但是仍占據(jù)UI空間),gone(隱藏,且不占UI空間) |
| android:progress | 設(shè)置進(jìn)度(對(duì)橫向進(jìn)度條有用),取值范圍:0-100 |
| style | 設(shè)置顯示樣式. "?android:attr/progressBarStyle":轉(zhuǎn)圈loading圖; "?android:attr/progressBarStyleHorizontal":橫向進(jìn)度條; "?android:attr/progressBarStyleHorizontal":橫向loading圖; |
更多屬性及實(shí)際效果,可以在開發(fā)工具里自行體驗(yàn).