本文实例为大家分享了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,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 >//点击外部隐藏</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; } });
效果图:
@H_301_30@
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的android popupwindow用法详解全部内容,希望文章能够帮你解决android popupwindow用法详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)