2.4 popupwindow 的使用及源码

popupwindow 的使用

popupwindow 常用api

1. 构造PopupWindow()对象

//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)

2. 展示PopupWindow()


//相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor):
//相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
showAsDropDown(View anchor, int xoff, int yoff):
//相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
showAtLocation(View parent, int gravity, int x, int y):

3. 其它Api

public void dismiss()
public void setFocusable(boolean focusable)
public void setTouchable(boolean touchable)
public void setOutsideTouchable(boolean touchable)
public void setBackgroundDrawable(Drawable background)

源码

从showAsDropDown(View anchor)说起

PopupWindow
|
|-showAsDropDown(View anchor, int xoff, int yoff, int gravity)
    |
    |- final WindowManager.LayoutParams p =createPopupLayoutParams(anchor.getApplicationWindowToken());
    |1. 创建WindowManager.LayoutParams
    |
    |
    |-preparePopup(p);
    |2. 准备Popup,包括创建mBackgroundView,mDecorView
    |
    |
    |-invokePopup(p);
    |3. 将decorView 通过mWindowManager.addView(decorView, p);添加window上
    

#1.createPopupLayoutParams
PopupWindow
|
|-createPopupLayoutParams(IBinder token)
    |
    |-final WindowManager.LayoutParams p = new WindowManager.LayoutParams();
    |创建 WindowManager.LayoutParams 实例
    |
    |
    |-p.gravity = computeGravity();
    |-p.flags = computeFlags(p.flags);
    |-p.type = mWindowLayoutType = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
    |-p.height = mLastHeight = mHeight;
    |- p.width = mLastWidth = mWidth;
    |这部分内容主要是设置WindowManager.LayoutParams 的属性,宽高,flgas,type等
    

#2.preparePopup
PopupWindow
|
|-preparePopup(p)
    |
    |-if (mBackground != null) mBackgroundView = createBackgroundView(mContentView);
    |-mBackgroundView.setBackground(mBackground);
    |-else mBackgroundView = mContentView;
    |如果mBackground !=null 会创建一个PopupBackgroundView赋值给mBackgroundView
    |否则,直接将mContentView赋值给mBackgroundView
    |
    |
    |-mDecorView = createDecorView(mBackgroundView);
    |创建PopupDecorView,PopupDecorView extends FrameLayout
    |重写dispatchKeyEvent(KeyEvent event),在按返回键的时候,会dismiss方法
    |重写了 onTouchEvent(MotionEvent event) 方法
    |在点击popup 外面的时候,会执行dismiss方法 if(event.getAction() == MotionEvent.ACTION_OUTSIDE)   dismiss();
    |
    |


#3.invokePopup
PopupWindow
|
|-invokePopup(p)
    |
    |
    |-mWindowManager.addView(decorView, p);
    |通过WindowManager将PopupDecorView 添加到Window上

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,025评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,859评论 2 59
  • 在Android中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利。关于弹窗的实...
    OzanShareing阅读 3,142评论 2 10
  • 这段时间,三岁多的丫头一到晚上关灯睡觉的时候便哭着吵着说怕。 “妈妈,我怕。” “怕...
    鱼闻乐阅读 433评论 6 2
  • 3.1 自我奖励 3.3.1 自我奖励及其次序的重要性 奖励是能使我们长期坚持的重要方法,而且这种方法不需要你付出...
    BGG阅读 543评论 0 3