Android TextView 跑马灯效果

要想实现一直循环的跑马灯效果,需要重写isFocused()方法使其一直处于获取焦点状态,并且onFocusChanged()里面不能调用父类的方法,否则跑马灯将会停止。下面是自定义的跑马灯TextView。

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

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

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

    @Override
    public boolean isFocused() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(true, direction, previouslyFocusedRect);
    }
}

在XML文件中使用方法,里面的这些属性不能缺少:

<com.gaoshijie.android.marqueetextview.MarqueeTextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever" />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工作中最近有用到,作下笔记,具体的代码片段如下: android:focusable和android:focusa...
    Viking_Den阅读 635评论 0 2
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,985评论 25 708
  • “假如你爱的人不爱你怎么办?”我经常听见别人问,也常常想问问别人。 落花有意,流水却无情,只能任由风吹向远方,最后...
    雨中的旅者阅读 260评论 1 5
  • “其次致曲。曲能友诚。诚则形,形则著。著则明。明则动。动则变。变则化。唯天下至诚为能化。” 退而说...
    钱江潮369阅读 637评论 0 3
  • 首先,欢迎越来越多的朋友加入到简书这个平台,在这里我们一起: 交流故事,沟通想法。 许多小伙伴是刚刚来到这个平台的...
    Charles远仁阅读 31,087评论 78 827