android textView跑马灯效果

来源:互联网 发布:小众手表 知乎 编辑:程序博客网 时间:2024/04/30 12:40

较长的文字不能换行显示,需要跑马灯效果
方法一:在XML文件中设置属性
注意maxlLines = “1”和singleLine=”true”的区别前者会将多余部分截去换到下一行显示,而后者是将内容全部放到一行只是屏幕无法显示而已所以此处应为后者。同理此处不能设置宽度为匹配父控件
做法:
1. 自定义控件

public class MarqueeText extends TextView {    public MarqueeText(Context context) {        super(context);    }    public MarqueeText(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }    @Override    public boolean isFocused() {        return true;//此处使得文本一直获得焦点    }    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {        //此处是为了防止焦点改变而效果消失    }}
  1. 应用自定义控件
<com.example.textviewsample.MarqueeText        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/text"        android:id="@+id/text_view1"        android:ellipsize="marquee"        android:singLine = "true"        android:focusable="true"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"/>        //使跑马灯一直不停
0 0