Android中ViewFlipper的施用

Android中ViewFlipper的使用

          看到一个程序员笔记里,有几句标语使用的是自动切换的模式,开始还以为做的是动画,看了源码才知道,使用的是ViewFlipper,在开发文档里,说的是简单的ViewAnimator ,使你添加的View动起来,在同一个时间只有一个View被展示出来,也可以设定好几个View轮流展示。

注意几个特别的设置就可以使用,android:flipInterval="2000",设置里面每一个View显示的时间,startFlipping()启动自动滑动过程,stopFlipping()停止自动化过程。

下边我把程序里的语句摘出来,单独写了个测试的应用。在.xml里面使用ViewFlipper,在ViewFlipper里面包含几个TextView,代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ViewFlipper
        android:id="@+id/flipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:flipInterval="2000" >

        <TextView
            android:id="@+id/text_1"
            style="@style/edittext_shadow_style"
            android:text="@string/animation_2_text_1" />

        <TextView
            android:id="@+id/text_2"
            style="@style/edittext_shadow_style"
            android:text="@string/animation_2_text_2" />

        <TextView
            android:id="@+id/text_3"
            style="@style/edittext_shadow_style"
            android:text="@string/animation_2_text_3" />

        <TextView
            android:id="@+id/text_4"
            style="@style/edittext_shadow_style"
            android:text="@string/animation_2_text_4" />
    </ViewFlipper>

</RelativeLayout>

然后再MainActivity里面直接找到ViewFlipper,启动就可以,代码:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flipper = (ViewFlipper) findViewById(R.id.flipper);
		flipper.startFlipping();
    }

然后就可以看到,ViewFlipper里面四个空间轮流显示的过程了。

代码:点我下载