TextView跑步灯效果及在特殊情况下无效的解决方式


概述:

关于在TextView中使用跑马灯效果的样例在网上一搜一大把。他们可能会让你像以下这样来在xml中定义TextView控件的属性。而事实也确是如此。

只是我不知道他们有没有遇到和我一样的问题(事实上我感觉是有的),我们第一次执行程序的时候。跑马灯没有效果,当我们关闭activity或是fragment再次进入的时候。跑马灯的效果又有了。


普通情况:


<TextView
                        android:id="@+id/textview1"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@id/main_has_connected_textView"
                        android:text="TextView"
                        android:singleLine="true"
                        android:ellipsize="marquee"
                        android:focusable="true"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:focusableInTouchMode="true"
                        android:scrollHorizontally="true"
                        android:textSize="22sp" />

改动之后:

如上的代码,一些主要的该设置的属性都已经设置好了。

只是还是会出现第一次执行无效果的情况。

这样的情况出现的原因应该是TextView在获得焦点的时候。会有丢失。我们能够动态地为这个TextView加入一些事件。

只是为了方便和安全性,我们能够将其放在它的自己定义控件中。

这个时候我们就须要在java代码中来动态实现了。

例如以下:

public class FlowTextView extends TextView {
    
    public FlowTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

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

}


xml中的使用与之前的无区别。例如以下:

<com.demo.widgets.FlowTextView
                        android:id="@+id/main_connect_fs_name"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:text="TextView"
                        android:singleLine="true"
                        android:textColor="#ffFFFFFF"
                        android:ellipsize="marquee"
                        android:focusable="true"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:focusableInTouchMode="true"
                        android:scrollHorizontally="true"
                        android:textSize="22sp" />



优质内容筛选与推荐>>
1、【11分钟训练完ImageNet】DNN训练再破纪录,1024CPUCaffe开源
2、什么才是Java的基础知识?
3、理解Java中锁的状态与优化
4、​都说高清地图重要,自动驾驶厂商们打算如何搞定它?
5、展望未来:使用PostCSS和cssnext书写CSS


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号