編輯:Android 開發實例
本文要帶大家實作一個仿支付寶錢包首頁、每個格子之間帶有分割線的 GridView,也就是俗稱的九宮格效果。先來看看成品長什麼樣子:

我們都知道,ListView 要加分割線非常容易,只要在版面配置裡設定 android:divider 與 android:dividerHeight 這兩個屬性即可。可惜 GridView 並沒有提供對應的屬性,那該怎麼辦呢?
在動手之前,我也參考過其他做法。例如自訂一個 GridView,在 dispatchDraw() 方法裡用演算法為每個 item 的四周畫上分割線。但實測後發現效果並不理想,有些 item 的線會畫不上去,很難精準達到預期。
其實思路很簡單:讓每個 item 的背景本身都帶上分割線,問題就迎刃而解了。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="none" > <com.finddreams.alipay.MyGridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:horizontalSpacing="0.0dip" android:listSelector="@null" android:numColumns="3" android:scrollbars="none" android:stretchMode="columnWidth" android:verticalSpacing="0.0dip" /> </ScrollView> </LinearLayout>
由於 GridView 的 item 數量可能較多,為了讓所有內容都放得下,通常會外層包一個 ScrollView。但這裡會遇到一個開發中很常碰到的經典問題:當 ListView 或 GridView 被嵌套進 ScrollView 後,往往只顯示第一行資料,後面的都不見了。
原因在於 GridView 與 ListView 本來會依據子 item 的寬高來決定自身大小,但一旦放進可上下滑動的 ScrollView,系統反而無法確定該畫多大,於是就出了問題。
這個問題網路上的解法很多,以下是 GridView 的處理方式:
public class MyGridView extends GridView { public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } } 接著我們定義一個 selector,讓 item 在不同狀態下呈現不同的背景:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><shape android:shape="rectangle"> <stroke android:width="1.0px" android:color="@color/line" /> <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" /> </shape></item> <item android:state_focused="true"><shape android:shape="rectangle"> <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" /> <stroke android:width="1.0px" android:color="@color/line" /> </shape></item> <item><shape android:shape="rectangle"> <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" /> <stroke android:width="1.0px" android:color="@color/line" /> </shape></item> </selector>
關鍵在於用一個矩形 rectangle 作為形狀,透過 stroke 描邊屬性把邊框顏色設為分割線的顏色,再於不同 state 的 item 裡設定不同的 gradient 漸層,就能呈現 item 被點擊或聚焦時的視覺回饋。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="0.0dip" android:background="@color/griditems_bg" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" android:background="@drawable/bg_gv" android:padding="12.0dip" > <ImageView android:id="@+id/iv_item" android:layout_width="58.0dip" android:layout_height="58.0dip" android:layout_centerHorizontal="true" android:contentDescription="@string/app_name" /> <TextView android:id="@+id/tv_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv_item" android:layout_centerHorizontal="true" android:layout_marginTop="5.0dip" android:maxLines="1" android:textColor="@color/commo_text_color" android:textSize="14.0sp" /> </RelativeLayout> </RelativeLayout>
最後,定義一個 Adapter 並把資料填充進 GridView 即可。這一步相信大家都已經很熟悉,這裡就不再贅述;若有不熟悉的地方,可以參考完整的專案範例。
Android 在地化實務:從 strings.xml 到語言切換
在地化的意義在地化不只是翻譯文字,也包括讓應用符合不同地區使用者的閱讀習慣。最常見的做法,是將畫面文字集中管理,並依語言提供不同資源。資源資料夾結構預設字串通常放在 re
Android 轉圈圈怎麼做?用不確定模式的 ProgressBar 表示「處理中」
有些工作你完全無法預估還要多久——查詢一筆資料、等待伺服器回應、初始化一個 SDK。這種時候顯示「已完成 60%」反而是騙人的,因為你根本不知道分母是多少。Android
ListView 分組實作:像通訊錄一樣按首字母分類
好的資訊呈現,講究的是「讓使用者一眼看到最有價值的部分」。而當資料量一大,分組就從加分項變成必需品。 分組的應用場合非常廣——只要是集合型資料,幾乎都適合分組。實作形式
Android MediaPlayer 完整教學:播放控制、進度條與背景播放的界線
MediaPlayer 是 Android 內建的媒體播放類別,能處理本機檔案、raw 資源與網路串流。它 API 不多、上手極快,但正因為簡單,很多人忽略它在背景播放與