Android开发之ViewSwitcher用法实例

 更新时间:2016年02月15日 10:15:18   作者:bigconvience  
这篇文章主要介绍了Android开发之ViewSwitcher用法,结合实例形式分析了ViewSwitcher的功能、使用方法与相关注意事项,需要的朋友可以参考下

本文实例讲述了Android开发之ViewSwitcher用法。分享给大家供大家参考,具体如下:

android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View。

ViewSwitcher的addView函数的代码如下:

/**
 * {@inheritDoc}
 *
 * @throws IllegalStateException if this switcher already contains two children
 */
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
  if (getChildCount() >= 2) {
    throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
  }
  super.addView(child, index, params);
}

可以看出,若View的数量超过两个,会抛出异常:java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory创建View或添加自己创建的View。

下面用一个例子介绍一下ViewSwitcher的用法。

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
       android:orientation="vertical"
       tools:context=".MainActivity" >
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" >
    <Button
        android:id="@+id/prev"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="previous" />
    <Button
        android:id="@+id/next"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="next" />
  </LinearLayout>
  <ViewSwitcher
      android:id="@+id/viewswitcher"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="- Button 2 -" />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="LinearLayout 2" />
    </LinearLayout>
  </ViewSwitcher>
</LinearLayout>

Activity的代码:

package com.example.AndroidTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
public class MyActivity extends Activity {
  Button buttonPrev, buttonNext;
  ViewSwitcher viewSwitcher;
  Animation slide_in_left, slide_out_right;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonPrev = (Button) findViewById(R.id.prev);
    buttonNext = (Button) findViewById(R.id.next);
    viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
    slide_in_left = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_in_left);
    slide_out_right = AnimationUtils.loadAnimation(this,
        android.R.anim.slide_out_right);
    viewSwitcher.setInAnimation(slide_in_left);
    viewSwitcher.setOutAnimation(slide_out_right);
    buttonPrev.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        viewSwitcher.showPrevious();
      }
    });
    buttonNext.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        viewSwitcher.showNext();
      }
    });
    ;
  }
}

实现效果图:

使用ViewSwitcher的setFactory设置切换的View,分为两步。

第一步:获得ViewSwithcer的实例

switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);

第二部:实现接口ViewFactory

switcher.setFactory(new ViewFactory()
{
  @Override
  public View makeView()
  {
    return inflater.inflate(R.layout.slidelistview, null);
  }
});

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android控件用法总结》、《Android短信与电话操作技巧汇总》及《Android多媒体操作技巧汇总(音频,视频,录音等)

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android 界面开发颜色整理

    Android 界面开发颜色整理

    本文主要介绍Android 界面开发的颜色,这里整理了很多颜色以供大家参考,希望Android 开发的工作者可以参考使用
    2016-07-07
  • 设置界面开发Preference Library数据重建机制详解

    设置界面开发Preference Library数据重建机制详解

    这篇文章主要为大家介绍了设置界面开发利器Preference Library数据重建机制详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Android实现状态栏白底黑字效果示例代码

    Android实现状态栏白底黑字效果示例代码

    这篇文章主要介绍了Android实现状态栏白底黑字效果的相关资料,实现后的效果非常适合日常开发中使用,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-10-10
  • Android中使用achartengine生成图表的具体方法

    Android中使用achartengine生成图表的具体方法

    这篇文章主要介绍了Android中使用achartengine生成图表的具体方法,有需要的朋友可以参考一下
    2014-01-01
  • Android 中倒计时验证两种常用方式实例详解

    Android 中倒计时验证两种常用方式实例详解

    这篇文章主要介绍了Android 中倒计时验证两种常用方式实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Android 线程死锁场景与优化解决

    Android 线程死锁场景与优化解决

    线程死锁是老生常谈的问题,线程池死锁本质上属于线程死锁的一部分,线程池造成的死锁问题往往和业务场景相关,本文主要介绍了Android 线程死锁场景与优化,感兴趣的可以了解一下
    2023-12-12
  • Java ArrayList源码深入分析

    Java ArrayList源码深入分析

    ArrayList 类是一个可以动态修改的数组,与普通数组的区别就是它是没有固定大小的限制,我们可以添加或删除元素。ArrayList 继承了 AbstractList,并实现了List接口
    2022-08-08
  • Android 内核代码 wake_up源码解析

    Android 内核代码 wake_up源码解析

    这篇文章主要为大家介绍了Android 内核代码 wake_up源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android开发的IDE、ADT、SDK、JDK、NDK等名词解释

    Android开发的IDE、ADT、SDK、JDK、NDK等名词解释

    这篇文章主要介绍了Android开发的IDE、ADT、SDK、JDK、NDK等名词解释,对这些概念搞不清楚是一件痛苦的事,本文就简洁讲解了这些名词的含义,一起扫盲吧,需要的朋友可以参考下
    2015-06-06
  • Android本地视频压缩方案的示例代码

    Android本地视频压缩方案的示例代码

    本篇文章主要介绍了Android本地视频压缩方案的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论