Android(安卓)ViewFlipper 用例
16lz
2021-01-26
[功能]
1. ViewFlipper 可以包含多个View 且View之间的切换有Animation 比如:渐变效果
[代码]
1. 创建包含ViewFlipper 的main.xml 还包含2个Button 用于各个View切换
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/previousButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Previous"
- />
- <Button
- android:id="@+id/nextButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Next"
- />
- </LinearLayout>
- <ViewFlipper
- android:id="@+id/flipper"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- >
- </ViewFlipper>
- </LinearLayout>
2. 设定 Animation 效果
- flipper=(ViewFlipper)findViewById(R.id.flipper);
- flipper.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
- flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
3. 在 ViewFlipper 里面增加各种View
- flipper.addView(addTextByText("HelloAndroid"));
- flipper.addView(addImageById(R.drawable.beijing_003_mb5ucom));
- flipper.addView(addTextByText("eoe.Android"));
- flipper.addView(addImageById(R.drawable.beijing_004_mb5ucom));
- flipper.addView(addTextByText("Gryphone"));
- ublicViewaddTextByText(Stringtext){
- TextViewtv=newTextView(this);
- tv.setText(text);
- tv.setGravity(1);
- returntv;
- }
- publicViewaddImageById(intid){
- ImageViewiv=newImageView(this);
- iv.setImageResource(id);
- returniv;
- }
4. View 切换
* 下一个View
- flipper.showNext();
* 上一个View
- flipper.showPrevious();
现释出所有代码!
over.
- MyFlipperUsage.rar (142.6 KB)
- 下载次数: 48
更多相关文章
- Andorid TabHost 使用小结
- Android(安卓)GridView控件 使用
- Android资料(书籍+代码)
- android 横竖屏切换,activity的生命周期
- 完美PopupWindow(记住用户名模拟)
- fragment内嵌fragment之间传值+切换fragment
- android Tween Animations(动画效果-代码实现)的使用
- Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法
- [置顶] android 框架