欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android popupwindow用法详解

程序员文章站 2023-11-14 17:34:52
本文实例为大家分享了android popupwindow的用法,供大家参考,具体内容如下 一、基本用法 一般做法,新建类继承popupwindow。例 /** *...

本文实例为大家分享了android popupwindow的用法,供大家参考,具体内容如下

一、基本用法

一般做法,新建类继承popupwindow。例

/**
 * popupwindow基本用法
 * created by administrator on 2015/11/25.
 */
public class demobasepop extends popupwindow {
  private linearlayout linear_layout;
  private textview dbp_text;
  private context context;
  public demobasepop(final activity context) {
    super(context);
    this.context = context;
    view view = layoutinflater.from(context).inflate(r.layout.demo_base_pop,null);
 
    setcontentview(view);
    setwidth(viewgroup.layoutparams.match_parent);
    setheight(200);
//    setheight(viewgroup.layoutparams.match_parent);
 
    setfocusable(true);
    setbackgrounddrawable(new bitmapdrawable());
    settouchable(true);
    setoutsidetouchable(true);
    setanimationstyle(r.style.popwin_anim_style);
//    setanimationstyle(0);   0是没有animation
 
    initview(view);
 
  }
 
  private void initview(view view) {
    dbp_text = (textview) view.findviewbyid(r.id.dbp_text);
  }
 
}

研究下popupwindow源码,以showasdropdown来讲

public void showasdropdown(view anchor, int xoff, int yoff) {
    if (isshowing() || mcontentview == null) {
      return;
    }
 
    registerforscrollchanged(anchor, xoff, yoff);
 
    misshowing = true;
    misdropdown = true;
 
    windowmanager.layoutparams p = createpopuplayout(anchor.getwindowtoken());
    preparepopup(p);
 
    updateaboveanchor(finddropdownposition(anchor, p, xoff, yoff));
 
    if (mheightmode < 0) p.height = mlastheight = mheightmode;
    if (mwidthmode < 0) p.width = mlastwidth = mwidthmode;
 
    p.windowanimations = computeanimationresource();
 
    invokepopup(p);
  }

第11行创建windowmanager.layoutparams。第12行preparepopup()中:

if (mbackground != null) {
      final viewgroup.layoutparams layoutparams = mcontentview.getlayoutparams();
      int height = viewgroup.layoutparams.match_parent;
      if (layoutparams != null &&
          layoutparams.height == viewgroup.layoutparams.wrap_content) {
        height = viewgroup.layoutparams.wrap_content;
      }
 
      // when a background is available, we embed the content view
      // within another view that owns the background drawable
      popupviewcontainer popupviewcontainer = new popupviewcontainer(mcontext);
      popupviewcontainer.layoutparams listparams = new popupviewcontainer.layoutparams(
          viewgroup.layoutparams.match_parent, height
      );
      popupviewcontainer.setbackgrounddrawable(mbackground);
      popupviewcontainer.addview(mcontentview, listparams);
 
      mpopupview = popupviewcontainer;
    } else {
      mpopupview = mcontentview;
    }

如果做了setbackgrounddrawable(new bitmapdrawable());那么mbackground则不为空,则会用popupviewcontainer作为mpopupview(即内容view)。而popupviewcontainer的dispatchkeyevent对返回键做了处理,按返回键后其中调用dismiss()方法。其ontouchevent对触摸事件做了处理,其源码:

public boolean ontouchevent(motionevent event) {
      final int x = (int) event.getx();
      final int y = (int) event.gety();
      <span style="font-family: 宋体; font-size: 9pt;">//点击外部隐藏</span>
      if ((event.getaction() == motionevent.action_down)
          && ((x < 0) || (x >= getwidth()) || (y < 0) || (y >= getheight()))) {
        dismiss();
        return true;
      } else if (event.getaction() == motionevent.action_outside) {
        dismiss();
        return true;
      } else {
        return super.ontouchevent(event);
      }
    }

系统做了这些处理,随之而来一个问题,如果我们要监听物理返回键该怎么办。看了上面的过程,我们可以想到将

setbackgrounddrawable(null);然后通过设置view的key监听,监听到后做相应的处理。
view.setonkeylistener(new view.onkeylistener() {
      @override
      public boolean onkey(view v, int keycode, keyevent event) {
        if (event.getkeycode() == keyevent.keycode_back) {
          if (event.getaction() == keyevent.action_down
              && event.getrepeatcount() == 0) {
            outanimator.start();
            return true;
          }
        }
        return false;
      }
    });

效果图:

android popupwindow用法详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。