Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 手機衛士(十一):自訂焦點 TextView 實作跑馬燈

編輯:Android 開發實例

跑馬燈效果原理

在 Android 中,TextView 要出現跑馬燈效果,通常需要單行顯示、設定 ellipsize 為 marquee,並讓元件取得焦點。若焦點不定時被搶走,文字就可能停止滾動。

建立自訂 FocusTextView

為了讓多個地方都能直接使用跑馬燈,可以建立一個自訂 TextView。做法很簡單:新增 FocusTextView 類別,繼承 TextView,並覆寫 isFocused 方法,讓它永遠回傳 true。

這樣一來,只要版面中使用這個自訂元件,文字就能持續滾動,而不需要每個地方都重複設定焦點。

版面使用方式

在 XML 中,把原本的 TextView 替換成 com.wuyudong.mobilesafe.view.FocusTextView,並保留 ellipsize、marqueeRepeatLimit、singleLine 等屬性。

自訂控件的標籤就是完整類別路徑,其他屬性使用方式與一般 TextView 相同。完成後,標題列下方的文字就能持續水平滾動。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved