zoukankan      html  css  js  c++  java
  • 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" />



  • 相关阅读:
    UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
    HDOJ(HDU).1412 {A} + {B} (STL SET)
    UVA.10474 Where is the Marble ( 排序 二分查找 )
    HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)
    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
    17 西安
    17 沈阳
    13 南京
    10/11 作战会议
    2019牛客国庆集训派对day5
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6744482.html
Copyright © 2011-2022 走看看