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
相关文章推荐
- Android自定义不需要焦点的TextView以实现跑马灯效果
- Android的自定义TextView实现跑马灯效果
- android自定义的TextView,实现跑马灯效果
- Android自定义textview实现竖直滚动跑马灯效果
- Android之TextView内容实现跑马灯效果
- Android TextView实现跑马灯效果
- Android TextView实现跑马灯效果
- android TextView实现跑马灯效果
- Android实现多个TextView同时显示跑马灯效果
- Android中TextView实现垂直滚动(轮换效果,非跑马灯)
- android用TextView实现跑马灯效果
- TextView属性android:ellipsize实现跑马灯效果
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- Android实现多个TextView同时显示跑马灯效果
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- android TextView不用获得焦点实现跑马灯效果 两种方法
- android 倒计时、有清除功能编辑框、popupwindow、跑马灯效果实现(基于TextView)
- Android重写TextView实现文字跑马灯效果
- android用TextView实现跑马灯效果
- 自定义TextView实现跑马灯效果