[Android]ViewSwitcher使用范例

前言

   虽然ViewSwitcher的中文API早已翻译出来,但一直没有在项目中使用过,也没有搜到很合适很简单的中文例子,这里与大家一起探讨和分享一下其用法。

 

声明

  欢迎转载,但请保留文章原始出处:) 

    博客园:http://www.cnblogs.com

农民伯伯: http://over140.cnblogs.com  

 

相关API

  Android 中文API (61) —— ViewSwitcher

 

正文

  一、简介

    ViewSwitcher适用于两个视图带动画效果的切换。这里实现两个视图切换的功能,并附带滑屏效果。

 

  二、截图

    视图一

    视图二

 

 

  二、范例代码

    带动画效果的切换视图一和视图二。

    xml

    <ViewSwitcher android:layout_alignParentBottom="true"
        android:persistentDrawingCache="animation" android:id="@+id/bottom"
        android:layout_width="match_parent" android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right" android:layout_height="122.0dip">
        <RelativeLayout android:layout_width="fill_parent"
            android:layout_gravity="bottom" android:layout_marginBottom="12.0dip"
            android:id="@+id/lyBottom" android:layout_height="wrap_content">
            <Button android:id="@+id/btn_pre" android:text="上一步"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_centerVertical="true" android:layout_toLeftOf="@+id/btn_middle"
                android:layout_marginRight="150.0dip" />
            <Button android:id="@+id/btn_middle" android:layout_width="wrap_content"
                android:layout_centerInParent="true" android:onClick="onClick" android:visibility="invisible"
                android:textSize="22.0sp" 
                android:layout_height="wrap_content"></Button>
            <Button android:id="@+id/btn_next"  android:text="下一步"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_centerVertical="true" android:layout_toRightOf="@+id/btn_middle"
                android:layout_marginLeft="150.0dip" />
        </RelativeLayout>
        <RelativeLayout android:layout_width="fill_parent"
            android:layout_gravity="bottom" android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView android:src="@drawable/cube" android:id="@+id/btn_reinsure"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="150.0dip"></ImageView>
            <ImageView android:src="@drawable/cyddz" android:id="@+id/btn_identity"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/btn_reinsure"
                android:layout_marginLeft="124.0dip"></ImageView>
            <ImageView android:src="@drawable/cykvmce" android:id="@+id/btn_insure_query"
                android:layout_toRightOf="@+id/btn_identity" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginLeft="140.0dip"></ImageView>
            <ImageView android:src="@drawable/m8gprs" android:id="@+id/btn_review"
                android:layout_toRightOf="@+id/btn_insure_query"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="140.0dip"></ImageView>
            <ImageView android:src="@drawable/th_appshareth"
                android:layout_toRightOf="@+id/btn_review"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="130.0dip"></ImageView>
        </RelativeLayout>
    </ViewSwitcher>

      代码说明:

        1. 这里使用系统自带的切换效果@android:anim/slide_in_left和@android:anim/slide_out_right。

        2. 请大家自行准备测试图片

    java

        bottom = (ViewSwitcher) findViewById(R.id.bottom);
        //切换为第一个
        bottom.setDisplayedChild(0);
        //切换到下一个
        //bottom.showNext()

转载:http://www.cnblogs.com/over140/archive/2011/09/03/2121526.html

时间: 2024-08-08 13:48:46

[Android]ViewSwitcher使用范例的相关文章

【Android开发】范例2-实现幻灯片式图片浏览器

我们来实现一个幻灯片式图片浏览器: 最下面一个画廊视图,选中画廊中的图片,会在上面的ImageSwitcher控件中显示大图. 效果图如图 实现方法: 在布局文件中添加图片切换控件ImageSwitcher和画廊视图控件Gallery res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas

【Android开发】范例1-开启新线程获取网络图片并显示到ImageView中

利用之前学过的多线程处理技术,我们来写一个开启新线程获取网络图片并显示到ImageView中的实例. 在布局文件中加入一个ImageView图片控件: res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t

【Android开发】范例4-猜猜宝石放在哪个箱子里

实现"猜猜宝石放在哪个箱子"的小游戏:主界面中有三个箱子,单击其中任意一个箱子,将打开箱子,显示里面是否有宝石,并且将没有被单击的箱子设为半透明显示,被单击的箱子正常显示,同时根据单击的箱子是否有宝石显示对应的结果.如果单击的箱子没有宝石,将显示"很抱歉,猜错了,要不要再来一次?"的提示文字,如果猜对了,就会将所有箱子透明化,并显示"恭喜您,猜对了,祝您幸福!" 效果图: 用户未选择箱子的时候: 用户选中带宝石的箱子的效果: 用户没有选中带宝石的

Android 布局简要范例

Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格. 而这些基础的布局框架结构很重要,需要玩的很熟悉.我将以前参考的部分代码示例,所做的相关实践在这里记录一下,稍后相关代码也会删除掉,这里做个备忘. 好了,现在开始: 我的代码结构是这样子的:       每个布局文件个类,如果要做具体的个性化,在其中的包里面扩展:   Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次

【Android开发】范例1-实现带描边的圆角图片

利用学过的BitmapShader渲染类,我们来实现一个带描边的圆角图片. 具体实现: 用来显示自定义的绘图类的布局文件 res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http:

【Android开发】范例2-实现带图标的ListView

效果如图: 实现代码: 首先编写res/layout/main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="w

【Android开发】范例2-实现放大镜效果

利用之前学过的图形图像绘画技术和图片添加特效技术,我们来实现一个Android放大镜的简单应用. 最终效果如图 具体实现: 用来显示自定义的绘图类的布局文件 res/layout/main.xml: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

【Android开发】范例1-显示在标题上的进度条

实现一个界面,实现在向页面添加图片时,在标题上显示一个水平进度条,当图片载入完毕后,隐藏进度条并显示图片 具体实现方法: res/layout/main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent&qu

【Android开发】范例3-&amp;quot;我同意&amp;quot;游戏条款

实现游戏开始界面中的我同意游戏条款功能:不勾选"我同意"复选框,就不会出现"开始游戏"的按钮,勾选"我同意"复选框,出现"开始游戏"的按钮. 效果图如图: 未点击"我同意"之前 点击"我同意"之后 点击"开始"按钮的瞬间的效果: 具体实现代码: res/layout/main.xml: <LinearLayout xmlns:android="http