xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ViewSwitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inAnimation="@anim/slide_in_top"
android:outAnimation="@anim/slide_out_bottom" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#123456"
android:text="11111111"
android:textSize="32sp" >
</TextView>
</RelativeLayout>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#FFFFFFFF"
android:text="22222222"
android:textSize="32sp" >
</TextView>
</RelativeLayout>
</ViewSwitcher>

<Button
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev" />

<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />

</LinearLayout>

自定义的动画,也可以用系统自带的,在@android:anim/目录:

slide_in_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="2000"
android:fromYDelta="-100%p"
android:toYDelta="0" />

</set>

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="2000"
android:fromYDelta="0"
android:toYDelta="100%p" />

</set>

Java代码:

package com.arnold.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private ViewSwitcher mSwitcher;
private Button btn_prev, btn_next;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSwitcher = (ViewSwitcher) findViewById(R.id.switcher);
mSwitcher.setDisplayedChild(0);

btn_next = (Button) findViewById(R.id.next);
btn_prev = (Button) findViewById(R.id.prev);

btn_next.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mSwitcher.showNext();
}
});
btn_prev.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mSwitcher.showPrevious();
}
});
}

}

注意:ViewSwitcher只可以在两种布局中切换,ViewFlipper可以应用多种布局,用法基本一样但后者多出几个方法:

isFlipping:用来判断View切换是否正在进行
setFilpInterval:设置View之间切换的时间间隔
startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
stopFlipping:停止View切换

使用ViewSwitcher和ViewFlipper在不同布局中切换的更多相关文章

  1. 在布局中使用android.support.v4.app.Fragment的注意事项

    1.Activity必须继承android.support.v4.app.FragmentActivity 2.fragment标签的name属性必须是完全限定包名,如下: <LinearLay ...

  2. DIV+CSS布局中主要CSS属性介绍

    Float: Float属性是DIV+CSS布局中最基本也是最常用的属性,用于实现多列功能,我们知道<div>标签默认一行只能显示一个,而使用Float属性可以实现一行显示多个div的功能 ...

  3. 响应式web布局中iframe的自适应

    困境 在响应式布局中,我们应该小心对待iframe元素,iframe元素的width和height属性设置了其宽度和高度,但是当包含块的宽度或高度小于iframe的宽度或高度时,会出现iframe元素 ...

  4. 在网页布局中合理使用inline formating context(IFC)

    引子:给大家出一个小小的考题,如何使用css来实现类似下面的在指定区域内,内容自适应的垂直居中.

  5. Android 布局中 如何使控件居中

    首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...

  6. CSS布局中的水平垂直居中

    CSS布局中的水平垂直居中 各位好,先说两句题外话.今天是我开通博客园的博客第一天,虽然我申请博客园的账号已经有一年半了,但是由于各种原因迟迟没有开通自己的博客.今天非常有幸开通博客,在此也写一篇关于 ...

  7. CSS弹性盒模型flex在布局中的应用

    × 目录 [1]元素居中 [2]两端对齐 [3]底端对齐[4]输入框按钮[5]等分布局[6]自适应布局[7]悬挂布局[8]全屏布局 前面的话 前面已经详细介绍过flex弹性盒模型的基本语法和兼容写法, ...

  8. 【转】在Android布局中使用include和merge标签

    内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...

  9. 几张图弄明白ios布局中的尺寸问题

    背景 先说说逆向那事.各种曲折..各种技术过时,老老实实在啃看雪的帖子..更新会有的. 回正题,这里讨论的是在Masnory框架下的布局问题.像我这种游击队没师傅带,什么都得自己琢磨,一直没闹明白下面 ...

随机推荐

  1. MacOSX和Windows 8的完美融合

    MacOSX和Windows8的完美融合 一般情况下我们要在MACOS系统下运行Windows软件怎么办呢?一种方法我们可以装CrossOver这款软件,然后在configuration->in ...

  2. HDOJ1010(BFS)

    //为什么bfs不行呢,想不通 #include<cstdio>#include<cstring>#include<queue>using namespace st ...

  3. tech

    流式计算框架storm.spark.genfire.esper(CEP)

  4. Windows phone 8 学习笔记(3) 通信(转)

    Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi.蓝牙.临近感应等多种方式.数据交互一般通过套接字来完成,我们将在本文详细的分析. 快速导航:一. ...

  5. 错误处理php

    一 HP关闭脚本错误提示的方法: 打开PHP安装目录下的php.ini文件 找到display_errors = On 修改为 display_errors = off 注意:如果你已经把PHP.in ...

  6. No deleted LINE chat messages recovered on iOS 9.1 after UFED extraction

    The evidence is iPhone 5s with iOS 9.1 and not jail breaked. I use UFED to do advanced logical extra ...

  7. 008sudo用户管理

    1.Sudo是Unix/Linux平台上一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的权利,让它们执行一些只有超级用户或其他特许用户才能完成的任务(主要体现为命令),比如,运行一些像mo ...

  8. 蘑菇街iOS客户端应用源码

    蘑菇街iOS客户端应用源码 随着蘑菇街由导购向电商转型,蘑菇街自己的IM也应运而生,IM起初只是用于商家和买家之间沟通的工具.后面我们问自己,既然已经有了用于客服的IM,为什么不自己做一个IM,用于公 ...

  9. url中文参数解决方案

    首先,弄清楚为什么url传递中文会转码或者乱码,以及http头 contentType="text/html; charset=GBK" 的作用. html代码会经过web服务器, ...

  10. 【转】Javascript 严格模式详解

    ref: http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscript 5添加 ...