Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android ProgressBar 四種樣式與常用屬性整理

編輯:關於 Android 程式設計

ProgressBar 有兩大類外觀:圓圈轉動的「不確定進度」模式,以及一格格填滿的「水平進度條」模式。透過 style 屬性切換,同一個控制項就能呈現完全不同的樣子。以下用一個版面檔把常見的四種都列出來。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <!-- 風格為大圓形進度條 -->
    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- 正常圓形進度條 -->
    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- 小圓形進度條 -->
    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true" />

    <!-- 橫向進度條:可加上 indeterminate 屬性,圓形加上則沒有效果 -->
    <ProgressBar
        android:id="@+id/progressBar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true" />

    <!-- 橫向進度條的進度狀態 -->
    <ProgressBar
        android:id="@+id/progressBar5"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="1000"
        android:progress="200"
        android:secondaryProgress="500" />

</LinearLayout>

四種 style

  • ?android:attr/progressBarStyleLarge:大尺寸圓形轉圈。
  • 不指定 style:中等尺寸的圓形轉圈,是最常見的寫法。
  • ?android:attr/progressBarStyleSmall:小尺寸圓形轉圈,適合放在標題列或列表項裡。
  • ?android:attr/progressBarStyleHorizontal:橫向長條進度條,可顯示百分比。

原範例把第 4、5 個都取名為 progressBar4,同一個版面裡出現重複 id 會造成 findViewById() 只能抓到其中一個,上面已改為 progressBar5。

indeterminate:不確定 vs 確定進度

android:indeterminate="true" 代表「不知道還要多久」,也就是一直轉圈的模式。這個屬性只在橫向樣式上有明顯意義;圓形樣式本身就是轉圈,加上去等於沒作用——這就是原始文章提到的那個現象。

要在程式碼中切換模式,用這兩個方法:

progressBar.setIndeterminate(false);   // 切換成可顯示百分比
progressBar.setProgress(600);          // 設定目前進度
progressBar.setMax(1000);              // 設定上限

secondaryProgress 的用途

android:secondaryProgress 會畫在主進度之上,顏色較淡。最典型的應用是網路音樂或影片播放器:主進度是「已播放位置」,次要進度是「已下載緩衝的位置」;另一個常見用法是安裝程式顯示「已下載 / 總大小」的雙層資訊。

動態建立時的樣式寫法

XML 用 style 屬性指定樣式,程式碼中則要改用 ProgressDialog 或建構 Activity 時的 theme,也可以在 Java 裡直接指定:

ProgressBar bar = new ProgressBar(this,
        android.R.attr.progressBarStyleHorizontal);
LinearLayout layout = findViewById(R.id.container);
layout.addView(bar);

關鍵是第二個建構子參數要傳 android.R.attr.progressBarStyleHorizontal,而不是字串樣式名稱。

本文原始作者:gongzibai

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved