Android中ViewFlipper的使用

来源:互联网 发布:mac终端格式化整个磁盘 编辑:程序博客网 时间:2024/06/02 20:07

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

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

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

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <ViewFlipper  
  7.         android:id="@+id/flipper"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_marginTop="10dp"  
  12.         android:flipInterval="2000" >  
  13.   
  14.         <TextView  
  15.             android:id="@+id/text_1"  
  16.             style="@style/edittext_shadow_style"  
  17.             android:text="@string/animation_2_text_1" />  
  18.   
  19.         <TextView  
  20.             android:id="@+id/text_2"  
  21.             style="@style/edittext_shadow_style"  
  22.             android:text="@string/animation_2_text_2" />  
  23.   
  24.         <TextView  
  25.             android:id="@+id/text_3"  
  26.             style="@style/edittext_shadow_style"  
  27.             android:text="@string/animation_2_text_3" />  
  28.   
  29.         <TextView  
  30.             android:id="@+id/text_4"  
  31.             style="@style/edittext_shadow_style"  
  32.             android:text="@string/animation_2_text_4" />  
  33.     </ViewFlipper>  
  34.   
  35. </RelativeLayout>  

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

[java] view plaincopy
  1.   public void onCreate(Bundle savedInstanceState) {  
  2.       super.onCreate(savedInstanceState);  
  3.       setContentView(R.layout.activity_main);  
  4.       flipper = (ViewFlipper) findViewById(R.id.flipper);  
  5. flipper.startFlipping();  
  6.   }  

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

代码:点我下载

0 0