popupwindow 捕获不到 setTouchInterceptor

xiaoxiao2021-03-01  14

红色的是关键两句。

PopupWindow window;

View v = this.getLayoutInflater().inflate(R.layout.popupwindow, null);

window = new PopupWindow(v, 200, 300);

 window.setOutsideTouchable(true);;//这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。当然这里很明显只能在Touchable下才能使用。不设置此项则下面的捕获window外touch事件就无法触发。

  变态

  Drawable win_bg = this.getResources().getDrawable(R.drawable.bg);

  window.setBackgroundDrawable(win_bg);

  这个很,即使在XML里设置了background也认为是没有背景,必须在这里指定背景,如果不指定同样无法触发下面的Touch监听事件。

  window.setTouchInterceptor(new OnTouchListener() {

  @Override

  public boolean onTouch(View v, MotionEvent event) {

  if (event.getAction() == MotionEvent.ACTION_OUTSIDE)

  window.dismiss();

  return false;

  }

  });

转载请注明原文地址: https://www.6miu.com/read-3849983.html

最新回复(0)