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

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

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

/***Returnstrueifthisviewhasfocus**@returnTrueifthisviewhasfocus,falseotherwise.*/

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

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

import android.content.Context;

import android.util.AttributeSet;

import android.widget.TextView;


/**

* 自定义TextView跑马灯的效果

*

* @作者: 刘倩</br>

* @时间 : 2014年5月29日 下午1:16:13</br>

* @描述: 主要定义TextView的跑马灯效果</br>

*/

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(安卓)Launcher源码研究(二) 加载app流程1
  2. Android(安卓)Java生成随机数的方法
  3. Android(安卓)PathMeasure的使用详解
  4. [转载]Android(安卓)ContentProvider和Uri详解 (绝对全面)
  5. Android中对/data/data//files下文件的读写操作
  6. Android(安卓)RecyclerView多布局的实现
  7. cocos2d-js如何在android平台上使用js直接调用Java方法
  8. Android(安卓)使用Pull方法解析XML文件的方法
  9. Android中的信使Messenger的源码解析

随机推荐

  1. Android的线程和内存模型
  2. Android(安卓)MediaScanner源代码解析
  3. Android 使用 Scheme 启动淘宝,天猫等其他
  4. Android中解析json数据的方式之一:Gson
  5. Android 用 libusb 操作 USB 设备,无须 ro
  6. 【Android】‘activity supporting actio
  7. adb开发工具合集(traceview and dmtracedu
  8. Android颜色渐变效果
  9. 使用 android studio 查看 android 源码
  10. android饼状图表