Android Kotlin TextView跑马灯效果

有时候我们在xml中配置跑马灯属性,最后却发现有时有效果,而有时却没有效果,这是为什么呢。

我们大概是如此配置的

android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"

这样配置是没有问题的,问题出在TextView上面,因为TextView焦点丢失了。

提示:maxLines属性不支持跑马灯


解决方案(来自阿里):

只需要重写父类方法IsFocused(),返回true,即可解决

override fun isFocused(): Boolean {
    return true
}


使用方法:与正常TextView使用方式相同

<com.example.work.myapplication.TextViewAlwaysMarquee
    android:id="@+id/marquee"
    android:layout_width="match_parent"
    android:text="Start123132132132132132132132132132132132123123End"
    android:layout_height="wrap_content" />


class源码

/**
 * Created by work on 2017/8/30.
 * 跑马灯
 * @author chris zou
 * @mail chrisSpringSmell@gmail.com
 */
open class TextViewMarquee : AppCompatTextView {
    constructor(context: Context) : super(context)

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)

    override fun isFocused(): Boolean {
        return true
    }
}

自动配置,无需在xml进行配置

/**
 * Created by work on 2017/8/30.
 * 跑马灯-无限循环
 * @author chris zou
 * @mail chrisSpringSmell@gmail.com
 */
class TextViewAlwaysMarquee : TextViewMarquee {

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        init()
    }

    override fun isFocused(): Boolean {
        return true
    }

    private fun init() {
        ellipsize = TextUtils.TruncateAt.MARQUEE
        marqueeRepeatLimit = -1
        setSingleLine()
    }
}




  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值