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

Android ListView 細節調校:去除陰影、透明底色與快速捲動條

編輯:關於 Android 程式設計

ListView 預設帶了幾個視覺效果,在大多數自訂設計的畫面裡反而顯得多餘:捲到頭尾時出現的黑色漸層陰影、拖動時閃出的黑底、點選瞬間的黃色高亮。這四行屬性就能把它們一一關掉,並加上右側的快速捲動條。

1. 移除頂部與底部的陰影

android:fadingEdge="none"

2. 移除拖動時的黑色底色

android:cacheColorHint="#00000000"

全透明的 color hint 等於告訴 ListView「背景沒有固定顏色,不必做優化快取」。這也是自訂背景或多色列舉時最常見的除錯寫法。

3. 移除選取時的黃色高亮

android:listSelector="#00000000"

4. 加上右側快速捲動條

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fastScrollEnabled="true"
    android:focusable="true" />

完整組合

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdge="none"
    android:cacheColorHint="#00000000"
    android:listSelector="#00000000"
    android:fastScrollEnabled="true"
    android:drawSelectorOnTop="false" />

補充說明

  • 若只想讓陰影變淡而非完全關閉,可用 android:fadingEdgeLength 調整長度。
  • fastScrollEnabled 需要列表項有字串型標籤才能正確跳轉,自訂 Adapter 時請實作 SectionIndexer 介面。
  • 新專案建議直接改用 RecyclerView,它內建 fastScrollEnabled、ItemDecoration 與更完整的回收機制,效能與彈性都優於 ListView。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved