http://blog.youkuaiyun.com/walker02/article/details/8561292
看到一个程序员笔记里,有几句标语使用的是自动切换的模式,开始还以为做的是动画,看了源码才知道,使用的是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实现视图自动切换效果。通过设置flipInterval属性控制每个视图的显示时间,并调用startFlipping()方法启动自动切换。示例中创建了包含多个TextView的ViewFlipper组件,展示了不同文本之间的轮播效果。
415

被折叠的 条评论
为什么被折叠?



