TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
效果图:
应用分为三步:
1.得到 TextSwitcher 实例对象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View
switcher.setFactory(this);
3.为switcher设定显示的内容,该方法执行,就会切换到下个View
switcher.setText(String.valueOf(new Random().nextInt()));
其中 要实现ViewSwitcher.ViewFactory中的makeView()方法
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
如果不适用ViewSwitcher.ViewFactory,也可以使用下面的方式代替
//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
代码:
package com.zhou.activity;
import java.util.Random;
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.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
//设置标题
setTitle("文字转换器");
//取得文字转换器
final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
// 指定转换器的 ViewSwitcher.ViewFactory,ViewSwitcher.ViewFactory会为TextSwitcher提供转换的View
switcher.setFactory(this);
//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// 设置转换时的淡入和淡出动画效果(可选)
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
// 单击一次按钮改变一次文字
Button btnChange = (Button) this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//为TextSwitcher设置显示内容,执行一次switcher.setText()方法,就会切换到下一个View
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
}
- 大小: 10.6 KB
- 大小: 10.4 KB
分享到:
相关推荐
Android 中textSwitcher与imageSwitcher的使用
主要为大家详细介绍了Android TextSwitcher文本切换器和ViewFlipper的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android TextSwitcher Demo 实例
主要介绍了Android TextSwitcher实现文字上下翻牌效果(铜板街),需要的朋友可以参考下
TextSwitcher是系统的原生类,调用它实现文字滚动效果非常流畅。
android-TextSwitcher-master.zip对你的学习会有帮助的
使用TextSwitcher实现多个文字从下到上轮播出现,点击后跳转到相对应界面
TextSwitcher:仿京东、淘宝滚动小广播,实现文字轮播滚动
图片轮播 文字轮播 ImageSwitcher TextSwitcher
TextView实现文字的跑马灯效果&TextSwitcher实现文字竖直飞出和飞入,使用的纯代码布局方式。
主要介绍了Android开发实现自动切换文字TextSwitcher功能,结合实例形式详细分析了Android使用TextSwitcher实现文字自动切换的原理、实现方法及相关操作注意事项,需要的朋友可以参考下
TextSwitcher:搜索框仿京东、淘宝滚动小广播,实现文字轮播滚动
自定义TextSwitcher实现垂直滚动效果,文章介绍https://blog.csdn.net/qizhenyibao/article/details/80817648
android官方的textswitcher demo,textview内容的切换(带动画效果)。
主要介绍了Android 上下滚动TextSwitcher实例详解的相关资料,需要的朋友可以参考下