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. x86_64 Ubuntu 14.04 LST安装gcc4.1.2 转载

    由于编译源码软件需要用到gcc4.1.2版本,但是本机已经安装有gcc4.8.4,下载gcc4.1.2源码编译总会出现运行找不到库文件错误,或者i386和x86_64不兼容问题,在http://ask ...

  2. sql实现分页

    IF EXISTS(SELECT * FROM sysobjects WHERE name='usp_getPage') DROP PROC usp_getPage GO CREATE PROC us ...

  3. [drp 7]转发和重定向的区别

    导读:类似于response.sendRedirect(request.getContextPath()+"/servlet/item/SearchItemServlet");和r ...

  4. 新安装的ubuntu系统如何设置中文输入法的方案

    本文是本人写的第一篇的linux博客,因为很菜,所以就把刚才自己安装中文输入法的过程给大家介绍一下,希望有所帮助. 1.首先,打开命令终端,两种方式,在Dash里面输入terminal然后enter, ...

  5. centos7 docker1.12 私有仓库

    docker1.12私有仓库 参考:http://www.cnblogs.com/xcloudbiz/articles/5497037.html 参考:http://www.07net01.com/2 ...

  6. npm命令大全

    参考网站:https://docs.npmjs.com/ 有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. npm 常用命令 npm install <name ...

  7. boost实现串口通信(一):小试牛刀

    /************************************************************************/ /* 功能:boost实现串口通信类 */ /* ...

  8. PHP 按二维数组的键值排序

    /** * 按二维数组的键值排序 * @param unknown $array 二维数组 * @param unknown $key 二维数组的键值 * @param string $order 升 ...

  9. javaSE第十九天

    第十九天    227 1:异常(理解)    227 (1) 定义    227 a)异常的引入    227 (2)异常的体系    228 (3)异常的处理:    229 A:JVM的默认处理 ...

  10. svn服务器搭建与配置

    http://www.cnblogs.com/armyfai/p/3985660.html