Android View遮罩无法拦截下方控件点击,求解决方法
Android遮罩无法拦截点击问题解决
你的问题核心是遮罩View默认不具备点击拦截能力,即使视觉上覆盖在控件上层,点击事件仍会穿透到下方控件。
解决方法很简单,给遮罩View添加以下属性:
android:clickable="true":让View具备消费点击事件的能力,拦截点击不再向下传递android:focusable="true"和android:focusableInTouchMode="true":确保在触摸模式下也能获取焦点,进一步避免事件穿透
修改后的布局代码:
<RelativeLayout> [all controls code here] <View android:layout_width="match_parent" android:layout_height="378dp" android:layout_alignParentStart="true" android:layout_alignParentBottom="true" android:layout_marginBottom="107dp" android:background="#86000000" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" /> </RelativeLayout>
补充说明:你设置的android:elevation="2000dp"其实在RelativeLayout中不是必须的——RelativeLayout的子View绘制顺序由XML中的顺序决定,最后声明的View会自动绘制在最上层,所以只要遮罩View在布局代码的末尾,视觉层级就已经是最高的了,elevation可以保留或移除,不影响拦截效果。
内容的提问来源于stack exchange,提问作者StealthRT
相关产品推荐
相关产品推荐

