目標:擴大ImageButton的點擊區域
遭遇問題:在布局文件xml中定義了一個ImageButton,因為圖標尺寸很小導致很難有效點擊。
具體代碼如下所示:
<ImageButton android:id="@+id/imageButtonAlbum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bt_album" android:contentDescription="@null"/>
其點擊區域如下圖所示(藍色邊框選中區域):
因為可點擊區域太小,導致很難點擊到Button,體驗非常不好。但是又不想擴大切片,也不想增加Java代碼,如何做?
解決方案:具體代碼如下
<ImageButton android:id="@+id/imageButtonAlbum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dip" android:src="@drawable/bt_album" android:background="@null" android:contentDescription="@null"/>
修改后效果圖如下:
結果:可准確並及時響應點擊行為。達到目標。:)
有用資料推薦:http://www.apkbus.com/android-23885-1-1.html
資料內容提要:Android4.0規定的有效可觸摸的UI元素標准是48dp,一般來說,48dp轉化為一個物理尺寸,約為9毫米。建議的目標大小為7~10毫米,這一點與iPhone一致,這是一個用戶手指能准確並且舒適觸摸的區域。
如圖所示,你的UI元素可能小於48dp,圖標僅有32dp,按鈕僅有40dp,但是他們的實際可操作焦點都達到了48dp的大小。