2023年7月15日发(作者:)
AndroidPopupWindow的使⽤和分析
Android PopupWindow的使⽤和分析
PopupWindow使⽤ PopupWindow这个类⽤来实现⼀个弹出框,可以使⽤任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
PopupWindow使⽤Demo 这个类的使⽤,不再过多解释,直接上代码吧。 ⽐如弹出框的布局:
Activity的布局中只有⼀个按钮,按下后会弹出框,Activity代码如下:package opupwindow;import ;import ty;import t;import ;import Inflater;import Event;import ;import kListener;import hListener;import Params;import ;import indow;import ;public class MainActivity extends Activity { private Context mContext = null; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); mContext = this; Button button = (Button) findViewById(); lickListener(new kListener() { @Override public void onClick(View view) { showPopupWindow(view); } }); } private void showPopupWindow(View view) { // ⼀个⾃定义的布局,作为显⽰的内容 View contentView = (mContext).inflate( _window, null); // 设置按钮的点击事件 Button button = (Button) ewById(1); lickListener(new OnClickListener() { @Override public void onClick(View v) { xt(mContext, "button is pressed", _SHORT).show(); } }); final PopupWindow popupWindow = new PopupWindow(contentView, _CONTENT, _CONTENT, true); chable(true); chInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { Log.i("mengdd", "onTouch : "); return false; // 这⾥如果返回true的话,touch事件将被拦截 // 拦截后 PopupWindow的onTouchEvent不被调⽤,这样点击外部区域⽆法dismiss } }); // 如果不设置PopupWindow的背景,⽆论是点击外部区域还是Back键都⽆法dismiss弹框 // 我觉得这⾥是API的⼀个bug kgroundDrawable(getResources().getDrawable( menu_bg_downward)); // 设置好参数之后再show DropDown(view); }}
弹出框的布局中有⼀个TextView和⼀个Button,Button点击后显⽰Toast,如图:
第⼀次实现的时候遇到了问题,就是弹出框不会在按下Back键的时候消失,点击弹框外区域也没有正常消失,搜索了⼀下,都说只要设置背景就好了。 然后我就找了个图⽚,果然弹框能正常dismiss了(见注释)。
PopupWindow源码分析 为了解答⼀下上⾯的问题,看看源码(最新API Level 19,Android 4.4.2)。1.显⽰⽅法 显⽰提供了两种形式: showAtLocation()显⽰在指定位置,有两个⽅法重载:public void showAtLocation(View parent, int gravity, int x, int y)public void showAtLocation(IBinder token, int gravity, int x, int y)
showAsDropDown()显⽰在⼀个参照物View的周围,有三个⽅法重载:public void showAsDropDown(View anchor)public void showAsDropDown(View anchor, int xoff, int yoff)public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) 最后⼀种带Gravity参数的⽅法是API 19新引⼊的。
弹出的⽅法中⾸先需要preparePopup(),最后再invokePopup()。 prepare的⽅法中可以看到有⽆背景的分别: /** *
Prepare the popup by embedding in into a new ViewGroup if the * background drawable is not null. If embedding is required, the layout * parameters' height is mnodified to take into account the background's * padding.
* * @param p the layout parameters of the popup's content view */ private void preparePopup(Params p) { if (mContentView == null || mContext == null || mWindowManager == null) { throw new IllegalStateException("You must specify a valid content view by " + "calling setContentView() before attempting to show the popup."); } if (mBackground != null) { final Params layoutParams = outParams(); int height = _PARENT; if (layoutParams != null && == _CONTENT) { height = _CONTENT; } // when a background is available, we embed the content view // within another view that owns the background drawable PopupViewContainer popupViewContainer = new PopupViewContainer(mContext); Params listParams = new Params( _PARENT, height ); kgroundDrawable(mBackground); w(mContentView, listParams); mPopupView = popupViewContainer; } else { mPopupView = mContentView; } mPopupViewInitialLayoutDirectionInherited = (LayoutDirection() == _DIRECTION_INHERIT); mPopupWidth = ; mPopupHeight = ; }
2.背景是否为空对Touch事件的影响 如果有背景,则会在contentView外⾯包⼀层PopupViewContainer之后作为mPopupView,如果没有背景,则直接⽤contentView作为mPopupView。 ⽽这个PopupViewContainer是⼀个内部私有类,它继承了FrameLayout,在其中重写了Key和Touch事件的分发处理: @Override public boolean dispatchKeyEvent(KeyEvent event) { if (Code() == E_BACK) { if (getKeyDispatcherState() == null) { return chKeyEvent(event); } if (ion() == _DOWN && eatCount() == 0) { cherState state = getKeyDispatcherState(); if (state != null) { racking(event, this); } return true; } else if (ion() == _UP) { cherState state = getKeyDispatcherState(); if (state != null && king(event) && !eled()) { dismiss(); return true; } } return chKeyEvent(event); } else { return chKeyEvent(event); } } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (mTouchInterceptor != null && h(this, ev)) { return true; } return chTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { final int x = (int) (); final int y = (int) ();
if ((ion() == _DOWN) && ((x < 0) || (x >= getWidth()) || (y < 0) || (y >= getHeight()))) { dismiss(); return true; } else if (ion() == _OUTSIDE) { dismiss(); return true; } else { return hEvent(event); } } 由于PopupView本⾝并没有重写Key和Touch事件的处理,所以如果没有包这个外层容器类,点击Back键或者外部区域是不会导致弹框消失的。
补充Case: 弹窗不消失,但是事件向下传递 如上所述: 设置了PopupWindow的background,点击Back键或者点击弹窗的外部区域,弹窗就会dismiss. 相反,如果不设置PopupWindow的background,那么点击back键和点击弹窗的外部区域,弹窗是不会消失的.
那么,如果我想要⼀个效果,点击外部区域,弹窗不消失,但是点击事件会向下⾯的activity传递,⽐如下⾯是⼀个WebView,我想点击⾥⾯的链接等.
研究了半天,说是要给Window设置⼀个Flag, _NOT_TOUCH_MODAL 看了源码,这个Flag的设置与否是由⼀个叫mNotTouchModal的字段控制,但是设置该字段的set⽅法被标记为@hide。 所以要通过反射的⽅法调⽤: /** * Set whether this window is touch modal or if outside touches will be sent * to * other windows behind it. * */ public static void setPopupWindowTouchModal(PopupWindow popupWindow, boolean touchModal) { if (null == popupWindow) { return; } Method method; try { method = laredMethod("setTouchModal", ); essible(true); (popupWindow, touchModal); } catch (Exception e) { tackTrace(); } } 然后在程序中: upWindowTouchModal(popupWindow, false); 该popupWindow外部的事件就可以传递给下⾯的Activity了。
Reference
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689431427a247215.html
评论列表(0条)