首先,使用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, 实现方法已经初现端倪了!

做法是这样:自定义一个OOXXTextView, 继承自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 RetainFragment状态保存的方法
  2. Android Volley框架使用方法详解
  3. H5页面调用android方法传json格式
  4. Android提交数据到服务的四种方法!!!
  5. [转载]android—OpenGL—GL10方法解析(转载)
  6. Android实现在xml文件中引用自定义View的方法分析
  7. Android 子线程更新UI的几种方法
  8. android加载字体内存泄漏的处理方法
  9. Android Studio运行main方法报错 SourceSet with name ‘main‘

随机推荐

  1. Android(安卓)的权限设置大全
  2. Android单元测试笔记
  3. Android实现与Apache Tomcat服务器数据交
  4. Android(安卓)Studio打包生成jar
  5. Android(安卓)之 自定义控件用法介绍
  6. Android:自定义控件和使用方法
  7. 竖着的seekbar
  8. 【翻译】(9)CPU Features
  9. Opengrok环境搭建--阅读Android源码
  10. BitMap、Drawable、inputStream及byte[]