您的位置:首页 > 其它

TextView文字横向滚动(跑马灯效果)

2012-12-29 15:29 591 查看
TextView实现文字滚动需要以下几个要点:

1、文字长度长于可显示范围:android:singleLine="true";

2、设置可滚到,或显示样式:android:ellipsize="marquee";
3、TextView只有在获取焦点后才会滚动显示隐藏文字,所以可以重写TextView类。(但是一直给予焦点可能会导致其不能被点击,如放在listView中的时候)

public class AlwaysMarqueeTextView extends TextView{

public AlwaysMarqueeTextView(Context context) {
super(context);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

//始终返回true,即一直获得焦点
@Override
public boolean isFocused() {
return true;
}
}


布局文件中的TextView添加:
android:singleLine
=“true”
android:focusable=“true”

android:marqueeRepeatLimit=“marquee_forever” //滚动次数,此时为无数次
android:ellipsize
=“marquee”

ellipsize属性

设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: