TextView跑马灯实现

本文详细介绍了如何实现TextView的跑马灯效果,包括设置`android:singleLine="true"`,`android:ellipsize="marquee"`,`android:focusable="true"`,`android:marqueeRepeatLimit="marquee_forever"`,以及在Java代码中设置`setSelected(true)`。为了保持焦点,需要创建一个自定义的MarqueeTextView并重写`isFocused()`方法。针对部分机型可能出现的文本不滚动问题,文章提到了使用反射机制修改`ViewConfiguration`的`mFadingMarqueeEnabled`属性,但需要注意反射操作应在`SetContentView()`之前进行。
摘要由CSDN通过智能技术生成

跑马灯实现要素:

1、android:singleLine="true";TextView的内容显示为一行。内容不满一行不滚动显示。内容超过控件长度,跑马灯显示。此处的属性只能选择singleLine,不能使用maxLines。

2、android:ellipsize="marquee"

3、android:focusable="true" 

4、android:marqueeRepeatLimit="marquee_forever"

5、java代码中可以设置musicName.setSelected(true);

6、需要TextView一直保持焦点。所以需要自定义一个TextView继承TextView,覆写isFocused方法,返回true,使焦点一直存在。

public class MarqueeTextView extends TextView {
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值