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

ViewFlipper 螢幕切換實作(一):用 XML 佈局與按鈕切換畫面

編輯:中階開發

要在同一個 Activity 內做「多頁」體驗,ViewFlipper 是最省事的元件之一:它把多個子檢視疊在同一個位置,一次只顯示一個,並可搭配動畫做進出效果。這篇先從最基礎的 XML 佈局與按鈕切換開始。

核心觀念:一個 ViewFlipper,多個子 LinearLayout

ViewFlipper 標籤內可以放多個子檢視,這裡每個 LinearLayout 就代表「一頁」。切換時整頁換掉,而不是只換其中一個元件。

版面配置檔 res/layout/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"
    android:background="#ffffff">

    <ViewFlipper android:id="@+id/details"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- 第一頁 -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff">

            <TextView android:id="@+id/tv_country"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="18px"
                android:text="Country" />

            <Spinner android:id="@+id/spinner_country"
                android:layout_width="200px"
                android:layout_height="55px" />

            <Button android:id="@+id/Button_next"
                android:layout_width="250px"
                android:layout_height="55px"
                android:textSize="18px"
                android:text="Next" />
        </LinearLayout>

        <!-- 第二頁 -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff">

            <TextView android:id="@+id/tv_income"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="18px"
                android:text="Income" />

            <EditText android:id="@+id/et_income"
                android:layout_width="200px"
                android:layout_height="55px" />

            <Button android:id="@+id/Button_previous"
                android:layout_width="250px"
                android:layout_height="55px"
                android:textSize="18px"
                android:text="Previous" />
        </LinearLayout>

    </ViewFlipper>
</LinearLayout>

這裡唯一需要牢記的規則就是:ViewFlipper 底下每個直屬子節點就是一頁,頁數與順序完全由 XML 中的撰寫順序決定。

Activity 程式碼

public class Activity1 extends Activity {

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

        // 為 Spinner 填入資料
        Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
        ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_dropdown_item,
                new String[] { "Canada", "USA" });
        spinnerCountries.setAdapter(countryArrayAdapter);

        // Next 按鈕:播放進場動畫後切到下一頁
        Button buttonNext = (Button) findViewById(R.id.Button_next);
        buttonNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                vf.setAnimation(AnimationUtils.loadAnimation(
                        v.getContext(), R.anim.push_left_in));
                vf.showNext();
            }
        });

        // Previous 按鈕:切回上一頁
        Button buttonPrevious = (Button) findViewById(R.id.Button_previous);
        buttonPrevious.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                vf.setAnimation(AnimationUtils.loadAnimation(
                        v.getContext(), R.anim.push_left_out));
                vf.showPrevious();
            }
        });
    }
}

切換的關鍵只有兩個方法:showNext() 前進一頁、showPrevious() 退回一頁。而 setAnimation() 決定切換時播放哪一段動畫,動畫檔放在 res/anim/ 下,SDK 已內建 push_left_in、push_left_out、slide_left、slide_right 等可直接使用。

幾個實務上的建議

用 dp 取代 px

範例中的 200px、55px 是像素單位,在不同解析度的裝置上會呈現不同的大小,這是早期教學常見的寫法。正式專案應改用 dip(或 dp)與 sp,才能在不同螢幕密度上有一致的視覺比例。

動畫方向要成對

「前進」與「後退」若用同一段動畫,操作起來會有方向錯亂的感覺。實務上通常準備兩組動畫:前進用 push_left_in / push_left_out,後退用 push_right_in / push_right_out。若覺得 push 系列過於強烈,換成 slide_right 往往更自然。

記住 ViewFlipper 與 ViewSwitcher 的差別

兩者都能切換子檢視,差別在於 ViewSwitcher 只接受兩個子檢視(常用於 View 與它的縮圖、或文字與編輯框的切換),而 ViewFlipper 沒有數量限制,適合多頁情境。若頁數很多且內容較重,也該考慮改用 ViewPager,因為它會在頁面不可見時銷毀離屏檢視,記憶體表現更好。

下一篇

用按鈕切換雖然直接,但使用者早就習慣了「用滑的」。下一篇要示範兩種手勢做法:一是透過 OnTouchListener 比較按下與放開的 X 座標差,二是用 GestureDetector 判斷 fling 手勢的速度與距離門檻。

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