編輯:關於 Android 程式設計
凡是需要「讓使用者等一等」的場景,幾乎都離不開進度條。本文延續《Android 從入門到精通》的系列進度,把 ProgressBar 的類別結構、常用方法與一個可直接跑起來的水平進度條範例一次講清楚。
ProgressBar 並不是一個全新的控件,它的血統非常單純:
java.lang.Object
└── android.view.View
└── android.widget.ProgressBar
也就是說,凡是 View 具備的繪製、測量、事件處理能力,它樣樣都有;在此之上,它只多做了一件事——把「完成度」畫成視覺化的長條或轉圈。
類別中方法不少,但日常開發真正高頻使用的其實只有下面幾個:


其中 setMax() 決定「總量」,setProgress() 決定「已完成量」,兩者搭配就構成進度條的全部語義;setSecondaryProgress() 則是第二軌道,常見於緩存與實際下載進度的雙層呈現。
這個範例做得很直觀:一個水平進度條,加上「前進/後退」以及「第二進度前進/後退」四顆按鈕,同時把數值同步到標題欄上,方便觀察變化。
public class MainActivity extends Activity {
private ProgressBar progressHorizontal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
// 標題欄進度的範圍是 0 ~ 10000
setProgress(progressHorizontal.getProgress() * 100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
findViewById(R.id.increase).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressHorizontal.incrementProgressBy(1);
setProgress(progressHorizontal.getProgress() * 100);
}
});
findViewById(R.id.decrease).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressHorizontal.incrementProgressBy(-1);
setProgress(progressHorizontal.getProgress() * 100);
}
});
findViewById(R.id.increase_secondary).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(1);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
}
});
findViewById(R.id.decrease_secondary).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(-1);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
}
});
}
}
這裡的關鍵是 incrementProgressBy():它接受的正負值就是「增量」,傳 -1 即可回退,比自己讀值再寫回要乾淨得多。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="30dp">
<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/decrease" />
<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/increase" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="@string/custom" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/decrease_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/decrease" />
<Button
android:id="@+id/increase_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/increase" />
</LinearLayout>
</LinearLayout>
這裡最容易忽略的是 style="?android:attr/progressBarStyleHorizontal"。不寫它,ProgressBar 預設會是小小的圓圈轉輪;寫上之後才會變成一條可以設定 max / progress 的水平長條。
<resources>
<string name="app_name">ProgressBar</string>
<string name="normal">預設進度條</string>
<string name="custom">自訂進度條</string>
<string name="decrease">減少</string>
<string name="increase">增加</string>
</resources>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.sweetlover.progressbar"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
進度條本身沒有太多學問,真正的重點在於「誰來推進它」。範例中用按鈕手動驅動,只是為了看清每一支 API 的效果;實務上更常見的是把 ProgressBar 綁到非同步任務的 onProgressUpdate,或在 ProgressBar 的子類 SeekBar 上做使用者互動。下一節我們再往自訂樣式與動畫前進。
Android UI 組件進階(一):打造帶進度條的按鈕
本系列是繼「Android UI 組件實例大全」之後的進階篇,將帶大家進一步深入學習各類 UI 組件。建議先讀完實例大全、掌握基礎組件的用法,再來學習本系列(當然你也可以
手把手實作:用自定義 View 畫出一個動態錯誤提示動畫
先上成果圖,讓大家有個直觀的認識: 建構子與 onMeasure 這部分屬於自定義 View 的例行功課,前面已經聊過很多次,這裡就快速帶過。 public Error
Android 版面配置優化:ViewStub、include、merge 使用與源碼分析
一、include:讓共用版面配置可重複使用 在 Android 介面開發中,隨著畫面數量增加,重複的標題欄、返回按鈕、搜尋列或提示區塊也會變多。若每個版面配置都重新撰寫
用程式碼讀取 Android 裝置資訊的完整做法
想在自己的 App 裡顯示手機的硬體與系統資訊嗎?本文以「關於手機」頁面為例,示範如何透過程式碼取得手機型號、Android 版本、處理器、內核與基帶等資料。 資料來源