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

Android自定义TextView实现跑马灯效果

2016-09-11 16:02 816 查看

TextView跑马灯效果

请遵守行规!

自己项目中用到,查资料的时候发现了很多方法,这里写下自己的用法,为了以后查看,也希望可以帮到用到的童鞋

第一种方法:在xml文件中直接写

如果在代码中直接使用textView自带的属性,需要在代码中对这个textView设置:setSelector(true);Android4.0之后必须加上 androidsingleLine=true

TextView androidlayout_width=400dip
androidlayout_height=wrap_content
androidlayout_marginLeft=80dip
androidlayout_marginBottom=25dip
androidtextSize=25sp
androidid=@+idtv_marquee
androidtextColor=@androidcolorblack

androidellipsize=marquee
androidfocusable=true
androidmaxLines=1
androidsingleLine=true
androidmarqueeRepeatLimit=marquee_forever
androidfocusableInTouchMode=true
androidscrollHorizontally=true

androidtext=这才是真正的文字跑马灯效果,驾~驾~……
androidbackground=#2FFFFFFF


第二种方法:自定义TextView类

写一个类继承自TextView,然后把在xml文件中需要设置的属性在代码中设置,因为跑马效果和焦点有冲突,所以重写了onFocusChanged方法,为的只是不让其执行父类的方法

public class MarqueeText extends TextView {
public MarqueeText(Context con) {
super(con);
}

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

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

@Override
public boolean isFocused() {
return true;
}

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {

}
}


下面是xml文件,在xml文件中直接引用即可

com.thunder.ktv.helper.MarqueeTexView
androidid=@+idtv_singerName
androidlayout_width=wrap_content
androidlayout_height=wrap_content
androidlayout_marginLeft=10dp
androidlayout_marginTop=10dp
androidbackground=@null
androidtext=歌手
androidtextColor=#d2d2d2
androidtextSize=16dp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: