如何实现跑马灯效果
2016-09-26 11:21
267 查看
往往看到一些应用的标题栏中当标题超出时便会自动滚动
这篇文章要讲的就是如何去实现TextView的Marquee效果
其实TextView已经自带了如何实现滚动的属性
通过上面的属性设置就能让TextView滚动起来。当然也可以通过代码去设置。
但是当设置完之后发现并没有滚动起来,原来TextView滚动的前提是这个空间必须要获得焦点。TextView需要必须处于focus状态。
在TextView的父类View中有一个方法isFocused(),系统通过这个方法去判断一个空间是否获得焦点。
所以我们就有了解决方案:
写一个子类继承TextView,重写isFocused()方法,直接返回true。当通过这个函数去判断TextView有没有获得焦点时,总是返回获得焦点于是我们的TextView就开始滚动起来了。代码很简单:
这篇文章要讲的就是如何去实现TextView的Marquee效果
其实TextView已经自带了如何实现滚动的属性
android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
通过上面的属性设置就能让TextView滚动起来。当然也可以通过代码去设置。
但是当设置完之后发现并没有滚动起来,原来TextView滚动的前提是这个空间必须要获得焦点。TextView需要必须处于focus状态。
在TextView的父类View中有一个方法isFocused(),系统通过这个方法去判断一个空间是否获得焦点。
所以我们就有了解决方案:
写一个子类继承TextView,重写isFocused()方法,直接返回true。当通过这个函数去判断TextView有没有获得焦点时,总是返回获得焦点于是我们的TextView就开始滚动起来了。代码很简单:
public class AlwaysMarqueeTextView extends TextView { /** * constructor * @param context Context */ public AlwaysMarqueeTextView(Context context) { super(context); } /** * constructor * @param context Context * @param attrs AttributeSet */ public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } /** * constructor * @param context Context * @param attrs AttributeSet * @param defStyle int */ public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; } }
相关文章推荐
- GET,POST,PUT,DELETE的区别
- 5分钟 搞定UIButton的文本与图片的布局
- Git快速入门
- 在树莓派上通过usb_cam将图像传输到笔记本电脑
- 想要空号检测软件检测结果更加准确 那么注意选择
- vs2013 搜索之正则表达式使用
- ListView原理分析
- 伸展树模板
- Android开发 ListView底部分割线不显示的解决办法
- Oracle 查看表空间的大小
- Android 手机卫士--九宫格使用
- 挖掘微信Web版通信的全过程
- document.body.offsetHeight等属性可能出现为0的时候
- 支持seaJs和requireJs的前端模块开发方案(二):业务页面和入口脚本init.js
- 查找学生
- 实例介绍因果图法
- Node.js学习(一)——Node.js的安装和express初步使用
- imx6 android源码下载方法
- 简历
- Timer中schedule()的用法