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.执行结果:

android TextSwitcher

android TextSwitcher

更多相关文章

  1. Android build.gradle 中 使用 buildConfigField 动态设置参数,de
  2. 界面编程之基本界面组件(7)ImageView(图像视图)
  3. Android用户界面 UI组件--ImageView及其子类ImageButton,QuickCo
  4. Android自定义视图二:如何绘制内容
  5. android studio 3.6.0 绑定视图新特性的方法
  6. android带消息红点的视图,超简洁的实现

随机推荐

  1. 【前端】js中a||b,a&&b 这种表示啥意思?
  2. 【前端】html5 video标签 自适应
  3. 【前端】手机号码输入框添加 空格
  4. 【前端】多页面有重复的html代码,怎么解决
  5. 【前端】在electron使用ckplayer播放rtmp
  6. 【前端】ios下获得焦点之后,页面布局上移
  7. 【前端】如何用JS判断网页在APP环境中打
  8. 【前端】关于vue的data,和return data的区
  9. 【前端】微信浏览器触摸事件流程
  10. 【前端】Promise的用法