FrameLayout的底层frame不能接受上层frame的点击事件

实现效果


方式是

让上层frame消费掉OnTouchEvent事件就可以了

提示这个frame层的顶层布局是LinearLayout,所以自己写一个LinearLayout,将这个LinearLayout的OnTouchEvent方法的返回值设为true就行了

package cn.yyp.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class CustomLinearLayout extends LinearLayout {

	public CustomLinearLayout(Context context) {
		super(context);
	}
	
	public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context,attrs);
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}
	
}

这样当上层frame显示的时候,下层frame是不能响应事件的,当然,上层frame必须覆盖掉下层frame层。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以在上层布局中设置一个透明的 View,并将其设置为可点击,然后将下层布局点击事件拦截掉。具体做法如下: 1. 在上层布局添加一个透明的 View,设置其宽高为 match_parent。 2. 将这个 View 设置为可点击,即设置 clickable 为 true。 3. 在下层布局中设置一个 OnTouchListener,拦截其点击事件。 4. 在 OnTouchListener 中判断该事件是否在上层布局View 上发生,如果是,则返回 true,表示已处理该事件,否则返回 false,表示未处理该事件。 示例代码如下: ``` // 上层布局 <FrameLayout android:id="@+id/layout_top" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/view_clickable" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" /> <!-- 其他布局内容 --> </FrameLayout> // 下层布局 <FrameLayout android:id="@+id/layout_bottom" android:layout_width="match_parent" android:layout_height="match_parent" android:onTouchListener="onBottomTouchListener"> <!-- 其他布局内容 --> </FrameLayout> // 下层布局 OnTouchListener public boolean onBottomTouchListener(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { int x = (int) event.getRawX(); int y = (int) event.getRawY(); Rect rect = new Rect(); mTopView.getGlobalVisibleRect(rect); if (rect.contains(x, y)) { return true; } } return false; } ``` 其中,mTopView上层布局中的 View,可以通过 findViewById(R.id.view_clickable) 获取。在下层布局中,可以通过设置 android:onTouchListener 属性为 onBottomTouchListener 来添加 OnTouchListener。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值