最近自己在寫自己的第一個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的,建議直接代碼賦值。