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

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 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值