今天遇到一个小问题。
如图这个CheckBox控件外面包裹了一个Relativelayout。
想让用户点击Relativelayout就触发CheckBox的点击事件选中事件。把点击事件给了Relativelayout。
本来没有什么问题 。点击的时候发现是可以触发checkbox的点击事件。但是在点击checkbox本身的时候。
并没有触发到Relativelayout的点击事件。这说明CheckBox自己触发了自己的点击事件。想到的解决办法
就是给CheckBox继续设置点击事件。让后处理的逻辑和Relativelayout的点击事件完全一样。
觉得这样做好麻烦。有没有什么好的办法。让CheckBox把点击事件给Relativelayout处理。后来查资料发现。
在布局文件中就可以解决这个问题。
<RelativeLayout android:id="@+id/rl_item_cb" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5" android:clickable="true"> <CheckBox android:id="@+id/cb_item" android:duplicateParentState="true" android:clickable="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@null" android:button="@drawable/check_box_select" /> </RelativeLayout>
如上图标红的属性:
给父布局设置:
android:clickable=“true”
给子控件设置:
android:duplicateParentState="true" //点击事件跟随父布局。
android:clickable="false" //禁掉子控件的点击事件