Android—ViewSwitcher使用範例

來源:互聯網
上載者:User

前言
   雖然ViewSwitcher的中文API早已翻譯出來,但一直沒有在項目中使用過,也沒有搜到很合適很簡單的中文例子,這裡與大家一起探討和分享一下其用法。
 
聲明
   
相關API
  Android 中文API (61) —— ViewSwitcher
 
本文
  一、簡介
    ViewSwitcher適用於兩個視圖帶動畫效果的切換。這裡實現兩個視圖切換的功能,並附帶滑屏效果。
 
  二、
    視圖一
    視圖二
 


 
  二、範例代碼
    帶動畫效果的切換視圖一和視圖二。
    xml
    <ViewSwitcher android:layout_alignParentBottom="true"
        android:persistentDrawingCache="animation" android:id="@+id/bottom"
        android:layout_width="match_parent" android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right" android:layout_height="122.0dip">
        <RelativeLayout android:layout_width="fill_parent"
            android:layout_gravity="bottom" android:layout_marginBottom="12.0dip"
            android:id="@+id/lyBottom" android:layout_height="wrap_content">
            <Button android:id="@+id/btn_pre" android:text="上一步"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_centerVertical="true" android:layout_toLeftOf="@+id/btn_middle"
                android:layout_marginRight="150.0dip" />
            <Button android:id="@+id/btn_middle" android:layout_width="wrap_content"
                android:layout_centerInParent="true" android:onClick="onClick" android:visibility="invisible"
                android:textSize="22.0sp"
                android:layout_height="wrap_content"></Button>
            <Button android:id="@+id/btn_next"  android:text="下一步"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_centerVertical="true" android:layout_toRightOf="@+id/btn_middle"
                android:layout_marginLeft="150.0dip" />
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent"
            android:layout_gravity="bottom" android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView android:src="@drawable/cube" android:id="@+id/btn_reinsure"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="150.0dip"></ImageView>
            <ImageView android:src="@drawable/cyddz" android:id="@+id/btn_identity"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/btn_reinsure"
                android:layout_marginLeft="124.0dip"></ImageView>
            <ImageView android:src="@drawable/cykvmce" android:id="@+id/btn_insure_query"
                android:layout_toRightOf="@+id/btn_identity" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginLeft="140.0dip"></ImageView>
            <ImageView android:src="@drawable/m8gprs" android:id="@+id/btn_review"
                android:layout_toRightOf="@+id/btn_insure_query"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="140.0dip"></ImageView>
            <ImageView android:src="@drawable/th_appshareth"
                android:layout_toRightOf="@+id/btn_review"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="130.0dip"></ImageView>
        </RelativeLayout>
    </ViewSwitcher>
      代碼說明:
        1. 這裡使用系統內建的轉場效果@android:anim/slide_in_left和@android:anim/slide_out_right。
        2. 請大家自行準備測試圖片
    java
        bottom = (ViewSwitcher) findViewById(R.id.bottom);
        //切換為第一個
        bottom.setDisplayedChild(0);
        //切換到下一個
        //bottom.showNext()

作者“農民伯伯”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.