Android开发之ViewSwitcher用法实例_Android

本文实例讲述了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
ViewSwitcher
android viewswitcher、android蓝牙开发实例、android开发实例、android app开发实例、android ndk开发实例,以便于您获取更多的相关知识。

时间: 2024-08-31 06:04:05

Android开发之ViewSwitcher用法实例_Android的相关文章

Android开发之ViewSwitcher用法实例

本文实例讲述了Android开发之ViewSwitcher用法.分享给大家供大家参考,具体如下: android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View. ViewSwitcher的addView函数的代码如下: /** * {@inheritDoc} * * @throws IllegalStateException if this switcher already contains two childre

Android开发之Service用法实例_Android

本文实例讲述了Android开发之Service用法.分享给大家供大家参考.具体分析如下: Service是一个生命周期较长而且没有界面的程序. 下面通过一个播放mp3的例子来学习. 先看MainActivity.java package com.example.servicetest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view

Android开发之Sqliteopenhelper用法实例分析_Android

本文实例讲述了Android开发之Sqliteopenhelper用法.分享给大家供大家参考.具体分析如下: 如果在安卓开发中,直接使用Activity中的openOrCreateDatabase(name, mode, factory)会有一系列跟随的问题.比如说数据库升级.更新等. 最好是使用其封装版本:SQLiteOpenHelper 继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个 方法. onCreate(SQLiteDatabase db) : 当数据库被首次

Android开发之TabActivity用法实例详解_Android

本文实例讲述了Android开发之TabActivity用法.分享给大家供大家参考,具体如下: 一.简介 TabActivity继承自Activity,目的是让同一界面容纳更多的内容.TabActivity实现标签页的功能,通过导航栏对各个页面进行管理. 二.XML布局文件 注意: 1.TabActivity的布局文件要求以TabHost作为XML布局文件的根. 2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>. 3.<TabWidg

Android开发之Sqliteopenhelper用法实例分析

本文实例讲述了Android开发之Sqliteopenhelper用法.分享给大家供大家参考.具体分析如下: 如果在安卓开发中,直接使用Activity中的openOrCreateDatabase(name, mode, factory)会有一系列跟随的问题.比如说数据库升级.更新等. 最好是使用其封装版本:SQLiteOpenHelper 继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个 方法. onCreate(SQLiteDatabase db) : 当数据库被首次

Android开发之Service用法实例

本文实例讲述了Android开发之Service用法.分享给大家供大家参考.具体分析如下: Service是一个生命周期较长而且没有界面的程序. 下面通过一个播放mp3的例子来学习. 先看MainActivity.java package com.example.servicetest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view

Android开发之TabActivity用法实例详解

本文实例讲述了Android开发之TabActivity用法.分享给大家供大家参考,具体如下: 一.简介 TabActivity继承自Activity,目的是让同一界面容纳更多的内容.TabActivity实现标签页的功能,通过导航栏对各个页面进行管理. 二.XML布局文件 注意: 1.TabActivity的布局文件要求以TabHost作为XML布局文件的根. 2.通常我们采用线性布局,所以<TabHost> 的子元素是 <LinearLayout>. 3.<TabWidg

Android编程开发之RadioGroup用法实例_Android

本文实例讲述了Android编程开发之RadioGroup用法.分享给大家供大家参考,具体如下: RadioGroup 有时候比较有用.主要特征是给用户提供多选一机制. MainActivity.java package com.example.lesson16_radio; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget

Android开发之Location用法实例分析_Android

本文实例讲述了Android开发中Location用法.分享给大家供大家参考,具体如下: Location 在Android 开发中还是经常用到的,如通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发)等.而在Android 中通过LocationManager来获取Location .通常获取Location 有GPS 获取,WIFI 获取. 这边介绍一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度. 第一步:创