Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 單行長文本水平滑動:用 EditText 簡易實現

編輯:關於 Android 程式設計

為什麼不用重寫 TextView

單行長文本需要水平滑動時,常見做法是繼承 TextView 並自行處理手勢,但其實有更簡單的方法:直接使用 EditText。

利用 EditText 的可滑動特性

EditText 因為需要編輯與移動游標,天生就能水平滑動。只要把背景設為透明、焦點關閉,並隱藏游標,就能呈現類似可滑動文本的視覺效果。

關鍵屬性

  • android:background="@android:color/transparent":讓背景透明。
  • android:focusable="false":避免取得焦點而進入編輯狀態。
  • android:cursorVisible="false":隱藏游標。
  • android:singleLine="true":保持單行。

適用情境

這種做法適合只需展示長文本、允許使用者左右滑動,但不需要真正編輯的場景。

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