首先,使用TextView实现走马灯形式的滚动显示,只需要对其设置两个属性:

android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"

但是,TextView的滚动显示,有一个前提,TextView需要必须处于focus状态。当TextView失去焦点的时候,TextView将会停止滚动。如何实现无限滚动,当然也需要从焦点入手。当然,直接requestFocus()是不行的,这里我使用了另外一个方法。观察到textView有一个名为isFocused()的方法,文档中的注释是这样的:

/*** Returns true if this view has focus** @return True if this view has focus, false otherwise.*/

也就是说当TextView拥有焦点的时候会返回true.同时可以发现,TextView中很多地方都是直接调用这个方法作为判断条件,最关键的,这个方法被声明为public! ok, 实现方法已经初现端倪了!

做法是这样:比如,现在写一个类命名为一个AlwaysMarqueeTextView, 继承自TextView, 同时override isFocused()方法,并使其返回值为true, 样例如下:

public class AlwaysMarqueeTextView extends TextView{ public AlwaysMarqueeTextView(Context context) { super(context); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; } }

更多相关文章

  1. Android中计算text文字大小的几个方法
  2. Android的DialogFragment的基本使用方法
  3. TextView的跑马灯效果,还有焦点问题
  4. Android ListView的item点击无响应的解决方法
  5. Android调用JNI出错 java.lang.UnsatisfiedLinkError: No implem
  6. android中使用jni,ndk的C语言回调方法

随机推荐

  1. Android的设计模式-桥接模式
  2. 50个Android开发人员必备UI效果源码
  3. Android的设计模式-迭代器模式
  4. Android的设计模式-访问者模式
  5. [Android]Android布局文件中的android:id
  6. Android的设计模式-命令模式
  7. Android的设计模式-单例模式
  8. 【学习推荐】最新Android学习教程持续更
  9. 9-30
  10. Android的设计模式-备忘录模式