TextView屬性android:ellipsize="marquee"不生效的解決辦法


最近自己在寫自己的第一個app,過程中遇到了這個問題,查了不少帖子,經過嘗試發現,這種問題一般分為兩類:

1. TextView的Text值賦值后不更改,很多帖子上說如下寫法就可以生效:

            <TextView
                android:id="@+id/music_name_tv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"   【必須】
                android:focusable="true"      【必須】
                android:focusableInTouchMode="true" 【必須】
                android:lines="1"              【必須】
                android:text="我的中國心我的中國心我的中國心我的中國心我的中國心我的中國心我的中國心我的中國心我的中國心xxxx"
                android:textColor="@color/colorAccent"
                android:textSize="15sp" />

2. TextView的文字動態賦值,這個時候直接寫在布局Xml里面已經不生效了,需要先給TextView賦值,然后再在代碼里面重新把屬性設置一遍:

    public static void setTextMarquee(TextView textView) {
        if (textView != null) {
            textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
            textView.setSingleLine(true);
            textView.setSelected(true);
            textView.setFocusable(true);
            textView.setFocusableInTouchMode(true);
        }
    }

備注:

1. 第一種情況經過測試,在我手機上不行,即使是TextView不動態賦值,仍舊不能滾動,猜測應該是系統兼容性問題。

2. 第二種經過驗證是OK的,建議直接代碼賦值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM