---
title: "Android 跑马灯"
author: "mmdev"
date: "2011-12-28T10:11:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2012-08-04T20:08:40Z"
id: 1620455
url: "https://mmdev.iteye.com/blog/1620455"
---

现在的人抄袭成风啊，都不带大脑思考的。

跑马灯的效果，我看到好多文章就想笑。很多文章都有一句，文字不能比TextView长。我了个去，有点脑子的都知道，文字没有TextView长的话还需要跑马吗？全显示出来了还需要跑马？

跑马灯的效果默认是需要获得焦点才有效。为了任何时候有效，可以继承Textiew，重写IsFocused方法，返回true。

```
public class MarqueeTextView extends TextView {

	public MarqueeTextView(Context context) {
		super(context);
	}
	public MarqueeTextView(Context context, AttributeSet attrs){
		super(context,attrs);
	}
	public MarqueeTextView(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
	}
	public boolean isFocused(){
		return true;
	}

}
```

然后TextView配置属性

```
mText.setSingleLine(true);
mText.setEllipsize(TruncateAt.MARQUEE);
```

```
mText.setMarqueeRepeatLimit(-1);
```

Ok了

![](http://hi.csdn.net/attachment/201112/29/0_1325120279kRr2.gif)

下载：[工程代码](http://download.csdn.net/detail/fylz1125/3979353)