TextView跑马灯效果(以及设置无效的坑)

第一步:设置跑马灯效果
给TextView添加以下属性

        android:ellipsize="marquee"  //文字显示不完全,以什么方式显示(这里就以滚动的行形式)
        android:focusable="true"  //获得焦点
        android:focusableInTouchMode="true"  //获得触摸焦点
        android:marqueeRepeatLimit="marquee_forever"  //滚动模式
        android:scrollHorizontally="true"  //横向滚动
        android:singleLine="true"  //以单行文本显示

问题: 正常来说就ok了,但是有时候控件太多可能获取不到焦点,导致跑马灯跑不起来。。。

第二步:解决问题(重写TextView)
把TextView的获取焦点方法直接返回true

public class MyTextView extends AppCompatTextView {
    public MyTextView(Context context) {
        super(context);
    }

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

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

    @Override   
    public boolean isFocused() {  //此方法直接返回true
        return true;
    }
}

这样使用自己的MyTextView再加上第一步中需要设置的属性就ok了!

<com.example.mynavi.widget.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="山东省济南市历城区舜泰中路2000号靠近中国工商银行(山钢支行)"
        android:textSize="16sp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
         android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        />

效果:


在这里插入图片描述
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容