編輯:Android 開發實例
當 App 要連線抓資料,使用者盯著沒有變化的畫面往往會不耐煩。這時候一個小小的旋轉動畫,就能明確傳達「系統正在忙,請稍候」。
Android 的 ProgressBar 同時涵蓋兩種樣式。第一種是長條形的進度條,適合已知工作總量的情境;第二種是持續旋轉的圓形 indicator,也就是本章要實作的 Spinner,專門用於「無法預估還要多久」的工作。
寫法與其說是「建立 Spinner」,不如說是建立一個套用特定 style 的 ProgressBar:
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
progressBarStyleLarge 是關鍵,它決定這個控制項會長成旋轉圓圈而非長條。系統另有 Small 與 Normal 兩種尺寸可選。
private ProgressBar spinner; spinner = (ProgressBar)findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE); spinner.setVisibility(View.VISIBLE);
實務上的節奏通常是:畫面載入時先設 GONE,使用者觸發工作時切換成 VISIBLE,工作結束後再改回 GONE。
除了可見性,ProgressBar 還提供這些方法:
| 方法 | 說明 |
|---|---|
isIndeterminate() | 查詢是否處於不確定模式。 |
postInvalidate() | 排入一次重繪。 |
setIndeterminate(boolean) | 切換不確定模式。 |
invalidateDrawable(Drawable dr) | 使指定 Drawable 失效並重畫。 |
incrementSecondaryProgressBy(int diff) | 遞增次要進度。 |
getProgressDrawable() | 取得進度模式的 Drawable 物件。 |
這個範例只有一顆按鈕與一個 Spinner,按下按鈕即顯示動畫。實機與模擬器皆可測試。
| 步驟 | 做法 |
|---|---|
| 1 | 用 Android Studio 建立專案 Spinner,Target SDK 選較新版本。 |
| 2 | 在 src/MainActivity.java 補上程式碼。 |
| 3 | 在 res/layout/activity_main.xml 加入 XML 元件。 |
| 4 | 在 res/values/string.xml 補字串。 |
| 5 | 執行並驗證。 |
主活動 src/com.yiibai.spinner/MainActivity.java:
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar spinner;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
spinner.setVisibility(View.GONE);
}
public void load(View view){
spinner.setVisibility(View.VISIBLE);
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
版面配置 res/layout/activity_main.xml:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button android:id="@+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_marginTop="103dp"
android:onClick="load" android:text="@string/hello_world" />
<ProgressBar android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/button1" android:layout_centerHorizontal="true" />
</RelativeLayout>
字串資源 res/values/string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Spinner</string>
<string name="hello_world">load spinner</string>
</resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.yiibai.spinner" android:versionCode="1" android:versionName="1.0" >
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.yiibai.spinner.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在已設定好的 AVD 上安裝並啟動,初始畫面只有一顆按鈕,Spinner 處於隱藏狀態:
按下 load spinner,旋轉動畫立刻出現在按鈕下方:
Android MediaPlayer 教學:播放、暫停與快轉倒轉
MediaPlayer 是 Android 中用來播放音訊與影片的常用類別。開發者可以透過它建立播放控制,例如播放、暫停、快轉與倒轉。 建立播放器 最直接的方式是使用 c
Android 編程實現泡泡聊天界面(附源碼)
本文分享一個實現 Android 泡泡聊天界面的完整例子,供大家參考。 前幾天寫了個界面,實現了類似微信的泡泡聊天效果。點擊發送按鈕後,屏幕就會顯示輸入框中的文字內容,運
Android 廣播、服務、數據庫、通知、包等核心術語圖解
簡介Android 中運用了大量機制,例如廣播、服務、數據庫、通知、包等等。本文用圖文並茂的方式,對這些核心概念做一個直觀的梳理。什麼是廣播?Broadcast 是一種在
Android 登入畫面實作:帳號驗證與錯誤次數鎖定
登入畫面是許多應用的必備環節,它負責向使用者索取帳號與密碼,以驗證身分並進入特定功能,例如登入 Facebook、微博等服務。本章將說明如何打造一個登入介面,並進一步處理