TextView跑马灯效果实现

如果想让textView做出跑马灯的效果,必须使用TextView的一个属性就是

android:ellipsize="marquee"
这个属性,但是这个属性的使用是需要这个控件是一直有焦点的,所以需要着自己进行自定义View的编写的,所以采用如下步骤:


1、自定义TextView

//获取焦点的textView
public class MyTtxtView extends TextView {

    //用代码new对象的时候会走此方法
    public MyTtxtView(Context context) {
        super(context);
    }

    //有属性的时候会走到这个地方,
    public MyTtxtView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //style的样式的话会走到这个方法
    public MyTtxtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //表示有没有获取焦点
    //跑马灯的话,,,首先需要调用这个方法判断是否有焦点
    //只有设置成true的话,跑马灯才会有效果
    //强制设置为有焦点

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

2、替换原来布局文件中的TextView,变成自己的自定义的TextView


    <com.lensman.a360.MyTtxtView
        android:id="@+id/advertisement"
        android:text="这是我第二次站在这里回味那种孤单了,也是最后一次。     上一次来的时候,是陈嫣然随我一道的。那是个刚下过雨的傍晚,一切都被雾气笼罩。
本文原创于鬼大爷鬼故事网,转载请注明原作者及出处,否则视为侵权。
原文链接:http://www.guidaye.com/dp/49028.html"
        android:lines="1"
        android:ellipsize="marquee"
        android:layout_below="@+id/main_title_tv"
        android:textColor="@color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

在这里面需要注意的是,必须将文本的行数设置成单独的一行


其实最简单的方法是如下的方法


android:focusable="true"
android:focusableInTouchMode="true"

加这两个字段就可以了



猜你喜欢

转载自blog.csdn.net/lovePaul77/article/details/73498095