TextView跑馬燈效果(以及設置無效的坑)

第一步:設置跑馬燈效果
給TextView添加以下屬性

        android:ellipsize="marquee"  //文字顯示不完全,以什么方式顯示(這里就以滾動的行形式)
        android:focusable="true"  //獲得焦點
        android:focusableInTouchMode="true"  //獲得觸摸焦點
        android:marqueeRepeatLimit="marquee_forever"  //滾動模式
        android:scrollHorizontally="true"  //橫向滾動
        android:singleLine="true"  //以單行文本顯示

問題: 正常來說就ok了,但是有時候控件太多可能獲取不到焦點,導致跑馬燈跑不起來。。。

第二步:解決問題(重寫TextView)
把TextView的獲取焦點方法直接返回true

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

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override   
    public boolean isFocused() {  //此方法直接返回true
        return true;
    }
}

這樣使用自己的MyTextView再加上第一步中需要設置的屬性就ok了!

<com.example.mynavi.widget.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="山東省濟南市歷城區(qū)舜泰中路2000號靠近中國工商銀行(山鋼支行)"
        android:textSize="16sp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
         android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"
        />

效果:


在這里插入圖片描述
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容