转自 http://jsf.iteye.com/blog/680257
http://www.devdiv.com/home.php?mod=space&uid=31865&do=blog&id=11360
http://blog.csdn.net/knlnzhao/article/details/7996969
http://www.2cto.com/kf/201207/144776.html
只记录一种方法了(文字长度超过控件长度,永久滚动;没有超出,不滚动)
走马灯的效果主要是通过android:singleLine,android:ellipsize,android:marqueeRepeatLimit属性来配置的。
android:singleLine=true ,表示使用单行文字,多行文字也就无所谓使用Marquee效果了。
android:marqueeRepeatLimit: 设置走马灯滚动的次数。
android:ellipsize: 设置了文字过长时如何切断文字,可以有none, start,middle, end, 如果使用走马灯效果则设为marquee.
但是Android的缺省行为是在控件获得Focus时才会显示走马灯效果,本例使用的Button,在某个Button获得焦点时Button上的文字才显示。
当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生TextView,重载isFocused 这个方法。
package com.example.looptext;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* 单行文本跑马灯控件
*
* @author admin
*
*/
public class ScrollForeverTextView extends TextView {
public ScrollForeverTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ScrollForeverTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollForeverTextView(Context context, AttributeSet attrs,int
defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.looptext.ScrollForeverTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="12345678901234567890"
android:textColor="#ff0000"
android:textSize="30px" />
</LinearLayout>