Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 實作 Market 風格的 Loading 效果:自訂版面取代 ProgressDialog

編輯:Android 開發實例

在 Android 裡做載入提示,直覺做法是 ProgressDialog。ApiDemos 中的 ProgressBar3.java 就示範了兩種典型用法:

但仔細看 Android Market 的載入畫面,發現它其實不是 ProgressDialog——而是一層鋪在畫面上的 Loading 版面,帶著一行說明文字。

效果既然不同,做法自然也要換。關鍵是:把 Loading 做成一個普通的版面檔,用可見性來控制顯示與隱藏。

一、建立 Loading 版面

res/layout/fullscreen_loading_indicator.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="horizontal"
    android:id="@+id/fullscreen_loading_style"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ProgressBar
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/current_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5.0dip"
        android:text="@+string/loading" />
</LinearLayout>

一個 ProgressBar 加上一行狀態文字,水平置中。這裡刻意沒用 ProgressDialog,因為 Dialog 會獨立成一個視窗、蓋在最上層且難以客製;做成版面則能完全融入自己的畫面結構。

二、在主版面中引入

main.xml:

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

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/main_info"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="abc" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/main_Loading"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <include
            android:visibility="visible"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            layout="@layout/fullscreen_loading_style" />
    </LinearLayout>
</LinearLayout>

結構是兩層並列:內容區(預設 gone)與 Loading 區(預設 visible)。切換時只要對調兩者的 visibility 就好,不必建立或銷毀任何物件。

三、主程式

package com.tymx.fullloading;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

public class myFullLoading extends Activity {
    private LinearLayout mLoadingLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    mLoadingLayout = (LinearLayout) findViewById(R.id.fullscreen_loading_style);
                    mLoadingLayout.setVisibility(View.GONE);

                    LinearLayout mMain = (LinearLayout) findViewById(R.id.main_info);
                    mMain.setVisibility(View.VISIBLE);
                }
            }
        };

        new Thread() {
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.print(i + "");
                }
                Message msg = handler.obtainMessage(1, "flash");
                handler.sendMessage(msg);
            }
        }.start();
    }
}

背景執行緒跑完工作後發訊息,主執行緒收到就切換可見性。這裡的 for 迴圈只是模擬耗時作業,實務上會換成真正的網路請求或檔案讀取。

四、執行結果

五、這種做法的價值與限制

相比 ProgressDialog,自訂 Loading 版面的優勢在於:可以放品牌圖、可以顯示多行狀態、可以依階段更新文字(把 current_action 的內容換掉即可),而且不會像 Dialog 那樣在旋轉螢幕時引發 lifecycle 相關的崩潰。

限制則是它需要你自行管理兩層版面的可見性,程式碼若散落在各處很容易出錯。實務上比較穩健的寫法是把 Loading 抽成一個自訂 View 或 Fragment,對外只暴露 show()、hide()、setMessage() 三個方法,讓呼叫方不必知道內部的版面結構。

另外提醒一個新版系統的注意事項:Android 已不鼓勵使用 ProgressDialog(官方文件直接標記為棄用),若只是想顯示不確定進度的忙碌狀態,ProgressBar 搭配 indeterminate="true" 放在自己的版面裡,正是本文這種思路的延伸,也是目前較受推薦的做法。

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