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

我们大概是如此配置的

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

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

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


解决方案(来自阿里):

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

override fun isFocused(): Boolean {    return true}


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

    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()    }}




更多相关文章

  1. android:configChanges属性,横竖屏切换
  2. android:scaleType 属性
  3. 【Android进阶】android:configChanges属性总结
  4. android api Demo之自定义Animation,实现3D旋转效果
  5. Android布局控件属性
  6. Android(安卓)Layout XML属性研究--android:layout_marginBottom
  7. android layout以及一些常用的android控件属性
  8. Android(安卓)组件属性
  9. Android中RelativeLayout各个属性的含义

随机推荐

  1. android 加载网络图片 SkImageDecoder::F
  2. 一起来学Android(安卓)Studio:(三)导入项目
  3. 在Eclipse中导入新浪微博SDK
  4. Android(安卓)Content Provider的应用
  5. Android语录
  6. 关于android系统Binder机制解析
  7. 深入Android(安卓)'M' Doze
  8. Android(安卓)消息机制之Message
  9. android笔记--android的进程与线程
  10. Android性能分析工具Systrace和TraceView