您的位置:首页 > 移动开发 > Android开发

android textView跑马灯效果

2016-07-01 10:57 393 查看
较长的文字不能换行显示,需要跑马灯效果

方法一:在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) {
//此处是为了防止焦点改变而效果消失
}
}


应用自定义控件

<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"/>
//使跑马灯一直不停
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android