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

Android 開發實作:打造仿支付寶錢包的九宮格 GridView

編輯: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

接著我們定義一個 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 被點擊或聚焦時的視覺回饋。

三、為 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 即可。這一步相信大家都已經很熟悉,這裡就不再贅述;若有不熟悉的地方,可以參考完整的專案範例。

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