android popupwindow用法详解
本文实例为大家分享了androidpopupwindow的用法,供大家参考,具体内容如下
一、基本用法
一般做法,新建类继承popupwindow。例
/**
*popupwindow基本用法
*CreatedbyAdministratoron2015/11/25.
*/
publicclassDemoBasePopextendsPopupWindow{
privateLinearLayoutlinear_layout;
privateTextViewdbp_text;
privateContextcontext;
publicDemoBasePop(finalActivitycontext){
super(context);
this.context=context;
Viewview=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(newBitmapDrawable());
setTouchable(true);
setOutsideTouchable(true);
setAnimationStyle(R.style.popwin_anim_style);
//setAnimationStyle(0);0是没有animation
initView(view);
}
privatevoidinitView(Viewview){
dbp_text=(TextView)view.findViewById(R.id.dbp_text);
}
}
研究下popupwindow源码,以showAsDropDown来讲
publicvoidshowAsDropDown(Viewanchor,intxoff,intyoff){
if(isShowing()||mContentView==null){
return;
}
registerForScrollChanged(anchor,xoff,yoff);
mIsShowing=true;
mIsDropdown=true;
WindowManager.LayoutParamsp=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){
finalViewGroup.LayoutParamslayoutParams=mContentView.getLayoutParams();
intheight=ViewGroup.LayoutParams.MATCH_PARENT;
if(layoutParams!=null&&
layoutParams.height==ViewGroup.LayoutParams.WRAP_CONTENT){
height=ViewGroup.LayoutParams.WRAP_CONTENT;
}
//whenabackgroundisavailable,weembedthecontentview
//withinanotherviewthatownsthebackgrounddrawable
PopupViewContainerpopupViewContainer=newPopupViewContainer(mContext);
PopupViewContainer.LayoutParamslistParams=newPopupViewContainer.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,height
);
popupViewContainer.setBackgroundDrawable(mBackground);
popupViewContainer.addView(mContentView,listParams);
mPopupView=popupViewContainer;
}else{
mPopupView=mContentView;
}
如果做了setBackgroundDrawable(newBitmapDrawable());那么mBackground则不为空,则会用PopupViewContainer作为mPopupView(即内容view)。而PopupViewContainer的dispatchKeyEvent对返回键做了处理,按返回键后其中调用dismiss()方法。其onTouchEvent对触摸事件做了处理,其源码:
publicbooleanonTouchEvent(MotionEventevent){
finalintx=(int)event.getX();
finalinty=(int)event.getY();
//点击外部隐藏
if((event.getAction()==MotionEvent.ACTION_DOWN)
&&((x<0)||(x>=getWidth())||(y<0)||(y>=getHeight()))){
dismiss();
returntrue;
}elseif(event.getAction()==MotionEvent.ACTION_OUTSIDE){
dismiss();
returntrue;
}else{
returnsuper.onTouchEvent(event);
}
}
系统做了这些处理,随之而来一个问题,如果我们要监听物理返回键该怎么办。看了上面的过程,我们可以想到将
setBackgroundDrawable(null);然后通过设置view的key监听,监听到后做相应的处理。
view.setOnKeyListener(newView.OnKeyListener(){
@Override
publicbooleanonKey(Viewv,intkeyCode,KeyEventevent){
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
if(event.getAction()==KeyEvent.ACTION_DOWN
&&event.getRepeatCount()==0){
outAnimator.start();
returntrue;
}
}
returnfalse;
}
});
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。