按照網上教程配置 TextView 跑馬燈(ellipsize="marquee"),設置 singleLine="true"、focusable="true"、focusableInTouchMode="true" 等屬性,且自定義 Marquee_TextView 重寫 isFocused() 返回 true,但文字始終不滾動。
<TextView
android:id="@+id/showSongName"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:textColor="#000000"
android:textSize="16sp"
android:focusable="true"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true" />
public class Marquee_TextView extends TextView {
@Override
public boolean isFocused() {
return true;
}
}
核心原因:TextView 寬度設置為 0dip 並配合 layout_weight,導致實際測量寬度可能小於或等於文字寬度。
ellipsize="marquee" 只有在文字寬度超過 TextView 可見寬度時才會觸發滾動。layout_width="0dip" + layout_weight 在線性佈局中會根據權重分配剩餘空間,若父佈局空間有限或權重極小,分配到的寬度可能不足以容納文字,反而文字完整顯示,不觸發跑馬燈。layout_width 固定為 100dip 後即可生效,證實是寬度問題。<TextView
android:layout_width="100dp" // 或 match_parent, 具體數值
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minWidth="120dp" // 確保有足夠寬度觸發跑馬燈
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true" />
TextView tv = findViewById(R.id.showSongName);
tv.setSelected(true); // 關鍵:必須調用 setSelected(true) 啟動跑馬燈
// 或在 onWindowFocusChanged 中請求焦點
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) tv.setSelected(true);
}
關鍵補充:即使寬度足夠,必須調用 setSelected(true) 或讓 TextView 獲得焦點,跑馬燈才會啟動。自定義 isFocused() 返回 true 並非標準做法,可能導致焦點系統混亂。
| 屬性 | 值 | 說明 |
|---|---|---|
ellipsize | marquee | 啟用跑馬燈省略模式 |
singleLine | true | 強制單行顯示(API 24+ 建議用 maxLines="1") |
marqueeRepeatLimit | marquee_forever | 無限循環滾動 |
focusable | true | 可獲取焦點 |
focusableInTouchMode | true | 觸摸模式下也可獲取焦點 |
scrollHorizontally | true | 允許水平滾動 |
match_parent 而非 0dp + weight 配置跑馬燈 TextView。onResume() 或 onWindowFocusChanged() 中調用 textView.setSelected(true) 確保啟動。setSelected()。isFocused(),改用標準焦點機制或 setSelected()。總結:TextView 跑馬燈不生效 90% 因寬度不足或未調用 setSelected(true)。確保 TextView 寬度小於文字寬度,並顯式啟動選中狀態,即可可靠實現跑馬燈效果。