android----TextView如何实现无限滚动显示
首先,使用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; } }
更多相关文章
- Android RetainFragment状态保存的方法
- Android Volley框架使用方法详解
- H5页面调用android方法传json格式
- Android提交数据到服务的四种方法!!!
- [转载]android—OpenGL—GL10方法解析(转载)
- Android实现在xml文件中引用自定义View的方法分析
- Android 子线程更新UI的几种方法
- android加载字体内存泄漏的处理方法
- Android Studio运行main方法报错 SourceSet with name ‘main‘