Android控件TextView之跑马灯功能问题记录

转载自:https://www.cnblogs.com/jesn/p/4298249.html

在使用TextView练习跑马灯时出现了以下问题:

为控件设置了以下属性

   <!--激活焦点-->
    android:focusable="true"
    <!--单行显示-->
    android:singleLine="true"
    <!--这里设置为超出文本后滚动显示-->
    android:ellipsize="marquee"
    <!--这个是设置滚动几次,这里是无限循环-->
    android:marqueeRepeatLimit="marquee_forever"
    <!--TouchMode模式的焦点激活-->
    android:focusableInTouchMode="true"
    <!--横向超出后是否有横向滚动条-->
    android:scrollHorizontally="true"

但是运行起来跑马灯的效果没有出现,由于是android小白,所以上网查原来是TextView默认第一个获取光标,其他的不默认获取,而TextView在没有获取光标的前提下,跑马灯的效果是不会出现的。
问题查到了,同时附录的也有解决办法,就尝试着按照大神说的办法做起来,发现确实解决了,所以记录下来。

解决办法:
1、创建一个class类
MarqueTextView继承TextView

public class marqueeText extends TextView {

    public marqueeText(Context context) {
        super(context);
    }

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

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

    //TextView默认设置是第一个获取到的光标,
    //如果想让所有的TextView都有跑马灯效果,则让所有的TextView都获取到光标就行了
    //这里return true 就是让所有的TextView都获取到光标
    @Override
    public boolean isFocused() {
        return true;
    }
}

2、回到需要设置跑马灯的TextView中,修改控件类型为新自定义的

再次运行,可以出现跑马灯效果出现了。

猜你喜欢

转载自www.cnblogs.com/qyxfirstblog/p/10142941.html