Android開發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例
前言
之前我們用過WebView類,打開網(wǎng)頁時就會出現(xiàn)加載網(wǎng)頁的情況,為了讓我們直觀的感受到網(wǎng)頁加載到什么程度而不是白白干等著空白頁,于是加載進(jìn)度條就是一個很好的展示方式,而通常情況下,當(dāng)我們的數(shù)據(jù)未加載完成時就會去使用進(jìn)度條,而ProgressBar就是用于界面上顯示進(jìn)度條的,下面讓我們看看這個控件的使用方法。
使用方法
首先我們將ProgressBar控件加到布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WebViewTest">
...
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
然后運(yùn)行程序就會發(fā)現(xiàn)我們的這個頁面上會加載這個進(jìn)度條的圈:


但我們會發(fā)現(xiàn),這個圈一直在加載,所以就需要我們對這個控件進(jìn)行控制,正常情況下應(yīng)該在數(shù)據(jù)未加載完成前顯示,加載完成后隱藏控件的,直接用setVisibility()方法來實(shí)現(xiàn)即可,我們這邊簡化一下,通過點(diǎn)擊頁面按鈕來顯示和隱藏:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/vWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/vClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_gravity="center"/>
</FrameLayout>
binding.vClick.setOnClickListener {
if (binding.progressBar.visibility == View.VISIBLE) {
binding.progressBar.visibility = View.GONE
} else {
binding.progressBar.visibility = View.VISIBLE
}
}
效果如下:

但是正常來說,web頁面我們一般不使用圈的方式,而是條形,其實(shí)很簡單,只要改ProgressBar控件在XML布局中的一個屬性:
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
/>
將它的style改為水平進(jìn)度條,進(jìn)度條最大長度設(shè)置為100,然后在Activity中代碼控制這個進(jìn)度長度即可:
binding.vClick.setOnClickListener {
// if (binding.progressBar.visibility == View.VISIBLE) {
// binding.progressBar.visibility = View.GONE
// } else {
// binding.progressBar.visibility = View.VISIBLE
// }
if (binding.progressBar.progress == 0) {
binding.progressBar.visibility = View.VISIBLE
}
binding.progressBar.progress = binding.progressBar.progress + 10
if (binding.progressBar.progress == 100) {
binding.progressBar.progress = 0
binding.progressBar.visibility = View.GONE
}
}
效果如下:

總結(jié)
本篇主要是介紹了進(jìn)度條ProgressBar控件的基本使用方法,這個控件的應(yīng)用場景非常多,而且現(xiàn)在也有很多封裝好更漂亮的控件,當(dāng)我們需要獲取數(shù)據(jù)去加載時就適合去展示一段加載圈形式的進(jìn)度條,再配合Dialog的效果,就能讓用戶感覺到數(shù)據(jù)的加載過程,這應(yīng)該也是數(shù)據(jù)可視化的一種應(yīng)用吧!
以上就是Android開發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例的詳細(xì)內(nèi)容,更多關(guān)于Android ProgressBar加載進(jìn)度條的資料請關(guān)注腳本之家其它相關(guān)文章!
- Android入門之彈出式對話框的實(shí)現(xiàn)
- Android入門之利用Spinner實(shí)現(xiàn)彈出選擇對話框
- Android對話框使用方法詳解
- Android自定義對話框的簡單實(shí)現(xiàn)
- 五分了解Android?Progress?Bar進(jìn)度條加載
- Android自定義View實(shí)現(xiàn)進(jìn)度條動畫
- Android實(shí)現(xiàn)簡單實(shí)用的垂直進(jìn)度條
- android實(shí)現(xiàn)簡單進(jìn)度條ProgressBar效果
- Jetpack Compose實(shí)現(xiàn)對話框和進(jìn)度條實(shí)例解析
相關(guān)文章
android實(shí)現(xiàn)圖片反轉(zhuǎn)效果
這篇文章主要介紹了android實(shí)現(xiàn)圖片反轉(zhuǎn)效果的方法,需要的朋友可以參考下2015-09-09
探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機(jī)制
一般情況下,我們都是用一些封裝好的網(wǎng)絡(luò)框架去請求網(wǎng)絡(luò),對底層實(shí)現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實(shí)現(xiàn),有益于我們對網(wǎng)絡(luò)加載進(jìn)行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復(fù)用機(jī)制來優(yōu)化網(wǎng)絡(luò)加載速度的原理與細(xì)節(jié)2021-06-06
Android空心圓及層疊效果實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android空心圓及層疊效果實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android TextView Marquee的應(yīng)用實(shí)例詳解
這篇文章主要介紹了Android TextView Marquee的應(yīng)用實(shí)例詳解的相關(guān)資料,這里說明使用方法及簡單實(shí)例和注意實(shí)現(xiàn),需要的朋友可以參考下2017-08-08
關(guān)于android studio升級4.1 某些插件使用不了的問題(Mac)
這篇文章主要介紹了關(guān)于android studio升級4.1 某些插件使用不了的問題(Mac),本文給大家分享解決方法供大家參考,感興趣的朋友跟隨小編一起看看吧2020-10-10
Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細(xì)介紹了Android使用ContentProvider實(shí)現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Android SharedPreferences的使用分析
本篇文章小編為大家介紹,Android SharedPreferences的使用分析。需要的朋友參考下2013-04-04

