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

GridView 屬性全解析:欄數、間距與自訂滾動條

編輯:關於 Android 開發

把 GridView 的常用屬性一次看明白

GridView 的樣子好不好看,幾乎全決定在幾個間距與滾動條屬性上。以下是一個較完整的設定範例:

<GridView
    android:id="@+id/movie_list"
    android:layout_width="906dp"
    android:layout_height="654dp"
    android:layout_marginLeft="230dp"
    android:layout_marginTop="110dp"
    android:paddingRight="60dp"
    android:background="@color/transparent"
    android:numColumns="5"
    android:verticalSpacing="80dp"
    android:horizontalSpacing="10dp"
    android:scrollbars="vertical"
    android:fadeScrollbars="false"
    android:listSelector="@drawable/item_selected"
    android:fadingEdge="none"
    android:scrollbarTrackVertical="@drawable/scrollbar_track"
    android:scrollbarThumbVertical="@drawable/scrollbar_thumb"
    android:transcriptMode="disabled" />

版面與間距

  • numColumns:欄數。可寫死數字,也可設為 auto_fit 讓系統依螢幕寬度自動決定。
  • verticalSpacing / horizontalSpacing:分別是列間距與欄間距。這兩個值直接決定了畫面看起來是鬆散還是擁擠。
  • columnWidth / stretchMode:前者指定每欄寬度,後者決定多餘空間如何分配(none、spacingWidth、columnWidth、spacingWidthUniform)。

滾動條相關

  • scrollbarTrackVertical:滾動條長條(軌道)的背景。
  • scrollbarThumbVertical:滾動條可拖曳短塊的背景。
  • scrollbarStyle:定義滾動條的樣式與位置,可設為 insideOverlay、insideInset、outsideOverlay、outsideInset。
  • scrollbarSize:滾動條尺寸,垂直時指寬度,水平時指高度。
  • fadeScrollbars:設為 true 時,停止滑動後滾動條會自動淡出隱藏。
  • fadingEdge:設定拉動滾動條時邊框漸變的方向。none 表示邊框顏色不變,horizontal 為水平方向變淡,vertical 為垂直方向變淡。

選取態與滾動行為

  • listSelector:item 被選中時的背景(或 selector)。
  • transcriptMode:希望最新加入的項目自動滾入可視範圍。共有 disabled、normal、alwaysScroll、transcript 四種;把 ListView/GridView 的資料更新後自動滑到底部,就是靠這個屬性。

實務提醒:自訂滾動條的 track 與 thumb 建議使用 nine-patch 圖,否則在 thumb 被拉長時容易出現邊緣變形。

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