編輯:Android 開發實例
在 Android 中,可以透過進度條向使用者顯示某項任務的執行進展。進度條主要分為兩種形態:進度列與旋轉式載入指示器(Spinner)。本章將聚焦於後者。
Spinner 適合用於那些「無法預估總耗時」的任務,用來表示目前有工作正在進行。要使用它,只需在 XML 中這樣定義即可。
<ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" />
在 XML 中定義好之後,還需要在 Java 檔裡透過 ProgressBar 類別取得它的參照,語法如下:
private ProgressBar spinner; spinner = (ProgressBar)findViewById(R.id.progressBar1);
之後就能透過 setVisibility 方法讓它隱藏,或在需要時再顯示出來,語法如下:
spinner.setVisibility(View.GONE); spinner.setVisibility(View.VISIBLE);
除了這些方法之外,ProgressBar 類別還定義了許多其他方法,能讓你更有效率地操作 Spinner。
以下範例示範如何以進度條實作 Spinner,打造一個點擊按鈕即可顯示載入指示器的應用。
你可以選擇在實體裝置或模擬器上執行這個範例。
以下是修改後主活動檔案 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) { // Inflate the menu; this adds items to the action bar if it is present. 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="action_settings">Settings</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" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <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>
接下來執行這個載入 Spinner 的應用。假設你已建立好 AVD 環境,先安裝並啟動它,若一切設定正確且程式沒有問題,就會看到如下的模擬器視窗:
現在點擊載入按鈕,即可開啟載入指示器,如下圖所示:

用 WebView 做一個極簡瀏覽器:網址列、跳轉與頁面攔截
WebView 是最快把網頁內容塞進 App 的方式。下面做一個只有網址列、跳轉鈕與網頁區的極簡瀏覽器。 一、程式碼 main.java package com.HHB
Android 多語系本地化實作:values-it 與字串資源
Android 的資源系統可以依裝置語言自動選擇字串。若要讓應用支援多語系,最直接的方式是建立不同語言的 values 資料夾。 建立 values-it 以義大利文為例
Android 轉圈動畫怎麼做:ProgressBar 的 Spinner 用法
當 App 要跑一段耗時的工作,使用者需要一個「你正在忙」的提示,而 ProgressBar 就是最直接的答案。它分成兩種外觀:橫向的進度條(progress bar),
Android 中 ListView 下拉刷新功能的完整實作解析
本文為大家剖析 Android 中 ListView 下拉刷新的實作方式,附上完整程式碼,值得參考,具體如下:下拉刷新是 ListView 中極為常見且實用的功能。看到不