編輯:關於 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 的處理方式——重寫 onMeasure:
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); } }
<?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>
在 selector 中設置形狀為矩形,並為矩形設置 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 中用 Pinyin4j 實作通訊錄拼音搜尋
Pinyin4j 嚴格說來是個 Java 開源庫,但在 Android 開發中被使用的頻率並不低。手機通訊錄的「搜尋聯絡人」功能,用 Pinyin4j 就能輕鬆實現——不
Android 音頻控制實作:用 MediaPlayer 播放搭配 AudioManager 調音量
播放音樂只是半件事,另一半是「讓使用者能控制這段音樂」——調大、調小、靜音。Android 把這兩件事分別交給兩個類別負責:MediaPlayer 管播放,AudioMa
如何預置 Android 手機 APK:四種情境完整教學
預置 APK 依需求可分為以下四種情境: 1. 如何將「帶源碼」的 APK 預置進系統? 2. 如何將「無源碼」的 APK 預置進系統? 3. 如何預置 APK 使得使用
《Android 開發藝術探索》讀書筆記:第三章 View 的事件體系
一、View 的基礎知識 什麼是 View View 是 Android 所有控件的基類,是界面層控件的一種抽象,代表一個具體控件。所有控件要麼是 View 的子公司,要