安卓開發(fā)入門教程-UI控件_ProgressBar

什么是ProgressBar

ProgressBar是用于提示用戶進(jìn)行等待的UI控件,.

基礎(chǔ)樣例

1.loading圖

效果圖

代碼

  1. 布局文件代碼
<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  1. 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)度條

效果圖

代碼

  1. 布局文件代碼
<ProgressBar
    android:id="@+id/horizontalProgressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progress="20" />
  1. 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).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容