1. 程式人生 > >關於RelativeLayout、LinearLayout新增點選事件失靈的問題

關於RelativeLayout、LinearLayout新增點選事件失靈的問題

       最近專案將近尾聲,昨天測試來告訴我,這個按鈕怎麼不太靈活。

 剛開始我還開玩笑說,對android手機寬容一些。今天早上為了證明,確實是android系統的問題,我打log測試,於是發現了一個意外的事情。

介面如下:

 

要點選的是切換房源這裡,為了增大點選面積:佈局如下

<RelativeLayout
android:id="@+id/switch_house"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center_vertical"
>
<ImageButton
android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/switch_house" /> </RelativeLayout>
但是我發現,點選的時候有時候起作用,有時候不起作用,log顯示的結果是有時候點選的時候,壓根就沒有走點選的方法。上網查了一下,有人說,外面包裹的點選事件RelativeLayout佈局的點選焦點有時候會被裡面的ImageButton搶佔,於是,有時候點選就失靈了。

我試著將裡面的ImageBtton換成ImageVIew。給外面的RelativeLayout+“屬性 focusableTouchMode ,然後執行,果然好使了。哈哈

修改後的佈局:

<RelativeLayout
android:id="@+id/switch_house"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center_vertical"
android:focusableInTouchMode="true"
>
<ImageView
android:layout_alignParentRight=
"true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/switch_house" /> </RelativeLayout>

一個小小的意外發現,希望給遇到類似問題的同學一些幫助!