突然需要TextView跑马灯效果,在网上找,找了半天,没有一个靠谱的,都是抄来抄去,我粘贴复制,都出不来效果,莫非是api更新了,
跑马灯效果没有了。
而且找到的blog,写的特别乱搞的跟我的博客有一拼了。甭管内容怎么样,一定要把博客排版弄好看。
自定义实现跑马灯效果!
我之所以出不来效果是因为textview foucus为true,而且在代码里设置,在布局文件里设置都是不管用的。只可以自定义TextView。
害我走了很多弯路。代码没几行,直接上代码。
public class AlawaysMarqueeTextView extends TextView {
public AlawaysMarqueeTextView(Context context) {
super(context);
init(context);
}
public AlawaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public AlawaysMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
setEllipsize(TextUtils.TruncateAt.MARQUEE);//设置横向滚动效果
setSingleLine(true);//设置单行显示
setMarqueeRepeatLimit(-1);//设置滚动次数无限次
}
@Override
public boolean isFocused() {//这个函数是关键 重写它 返回true
return true;
}
}