public class TextSwitcher extends ViewSwitcher

java.lang.Object

android.view.View

android.view.ViewGroup

android.widget.FrameLayout

android.widget.ViewAnimator

android.widget.ViewSwitcher

android.widget.TextSwitcher

ImageSwitcher 和TextViewSwitcher都是ViewSwitcher 的子类,ViewSwitcher又是ViewAnimator 的子类,ViewAnimator (FrameLayout的子类)提供了不同View之间切换时的动画效果支持,而ViewAnimator 为FrameLayout的子类,因此ViewAnimator中包含的子View都是叠放在一起,一般情况下支持看到最上面的一个View。

ViewSwitcher只能最多包括两个子View。每次只显示其中一个View,它有两个子类TextSwitcher 和ImageSwitcher 。本例介绍TextSwitcher ,TextSwitcher 种能包含TextView,TextSwitcher主要可以用法文字切换时动画效果。

每当调用setText时,TextSwitcher 使用设定的动画效果显示新文字串和原先文字之间的切换。可以使用addView 为ViewSwitcher 手动添加一个View到ViewSwitcher中,也可以使用ViewSwitcher.ViewFactory 自动创建一个新View。本例使用ViewSwitcher.ViewFactory为TextSwitcher创建一个新View。

构造函数

public TextSwitcher (Context context)

创建一个新的空TextSwitcher

参数

context 应用程序上下文

public TextSwitcher (Context context, AttributeSet attrs)

使用提供的contextattributes来创建一个空的TextSwitcher

参数

context 应用程序环境

attrs 属性集合

公共方法

public void addView (View child, int index, ViewGroup.LayoutParams params)

根据指定的布局参数新增一个子视图

参数

child 新增的子视图

index 新增子视图的位置

params 新增子视图的布局参数

抛出异常

IllegalArgumentException 当子视图不是一个TextView实例时

public void setCurrentText (CharSequence text)

设置当前显示的文本视图的文字内容。非动画方式显示。

参数

text 需要显示的新文本内容

public void setText (CharSequence text)

设置下一视图的文本内容并切换到下一视图。可以动画的退出当前文本内容,显示下一文本内容。

参数

text 需要显示的新文本内容

案例:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextSwitcher         android:id="@+id/textviewswitcher_id"        android:layout_width="fill_parent"        android:layout_height="wrap_content"                >     </TextSwitcher>         <Button         android:id="@+id/btn_textviewswitcher"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextSwitcher"        /></LinearLayout>

package com.test;import android.R.string;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher.ViewFactory;public class TextSwitcherDemo extends Activity implements ViewFactory {private  TextSwitcher  textSwitcher;private  int counter=0;private Button   button; @Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.textswitcher);textSwitcher=(TextSwitcher)findViewById(R.id.textviewswitcher_id);textSwitcher.setFactory(this);Animation in = AnimationUtils.loadAnimation(this,  android.R.anim.fade_in);  Animation out = AnimationUtils.loadAnimation(this,  android.R.anim.fade_out);  textSwitcher.setInAnimation(in);  textSwitcher.setOutAnimation(out); button = (Button)findViewById(R.id.btn_textviewswitcher);button.setOnClickListener(listener);updateCounter();}private OnClickListener  listener=new OnClickListener() {@Overridepublic void onClick(View v) {counter++;updateCounter();}};private void updateCounter() {// TODO Auto-generated method stubtextSwitcher.setText(String.valueOf(counter));}@Overridepublic View makeView() {// TODO Auto-generated method stubTextView  textView = new TextView(this);textView.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);textView.setTextSize(35);return textView;}}

3.执行结果:

更多相关文章

  1. Android(安卓)build.gradle 中 使用 buildConfigField 动态设置
  2. Android手机客户端与Servlet交换数据
  3. Android开发中Android与网页(JavaScrip)t的交互
  4. Understanding Android's LayoutInflater.inflate()
  5. android中的UI控制(一)
  6. Android实现书籍翻页效果--扩展版
  7. Android实现书籍翻页效果--扩展版(转)
  8. Android(安卓)MVC模式
  9. 【Unity3D】与Android相互传递消息

随机推荐

  1. Android(安卓)ActionBar的源代码分析(三)
  2. 万树:Java和Android有什么联系?有什么区别
  3. Android源码分析:HeaderViewListAdapter
  4. Android面试心得和应对思路
  5. Android(安卓)开发未来的出路何在? | 技术
  6. Android(安卓)Layout布局文件里的android
  7. Android(安卓)2.3 Dev Guide (55)-- Andr
  8. 使用Chrome://inspect调试 Android(安卓)
  9. Android加密与iOS加密技术分析
  10. 移动开发参考书之Android篇