ViewSwitcher使用例子
来源:互联网 发布:embed.js 编辑:程序博客网 时间:2024/05/22 06:09
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);
- }
- });
1 0
- ViewSwitcher使用例子
- ViewSwitcher的简单使用
- ViewSwitcher的简单使用
- ViewSwitcher使用范例
- ViewSwitcher
- ViewSwitcher
- android中ViewSwitcher的使用
- Android ViewSwitcher简介和使用
- Android中viewSwitcher的使用
- Android ViewSwitcher简介和使用
- android使用ViewSwitcher实现视图切换
- ViewAnimator及其子类ViewSwitcher,ViewFipper的使用
- 使用ViewSwitcher和ViewFlipper在不同布局中切换
- 使用ViewSwitcher实现ListView的数据动态加载[学习]
- 使用ViewSwitcher模拟手机屏幕应用分屏和切换
- Android开发中ViewFlipper和ViewSwitcher使用GestureDetector完成切换
- 使用ViewSwitcher和ViewFlipper在不同布局中切换
- ViewFilpper ViewSwitcher
- socket
- 额...
- 从出租司机到SAP CTO
- 64位服务器IIS7.5运行ASP网站连接数据库错误解决方案
- Fragment 之间防止重复加载
- ViewSwitcher使用例子
- cocos2d初级篇笔记二
- 通过下标遍历std::vector,一个非常隐蔽的越界错误
- 游戏服务器场景管理AOI方案
- 新花生壳nat123哪个好,使用对比详细功能解密
- 进行Mahout0.8聚类研究时,突然出现无法连接服务器问题,解决方案
- 黎明前的黑暗---开启机房收费重构
- 从文件读取字符串,并求出该字符串包含的字符集中所有字符的全部组合
- iOS微信支付和支付宝支付开发详解