前记:
本人菜鸟一枚,第一次写博客,写得不好。不喜勿喷,有疑问的地方可以留言回复~。
PopupWindow的使用思路:
在说明他的相关使用方法之前,我们先理清一下它的使用思路。以方便我们针对性的研究各个问题。
一· 我们大致会使用PopupWindow的场景:
1.希望点击某些东西之后,能弹出一个独立的操作区域,以承载更多的内容。
2.满足一些比较不错的设计效果。
例如下面将提示删除的地方做成一个卡片的形状,并且再赋给它一个动画,那么它会满足material design的设计风格。一个卡片进入界面时从低端滑倒中端进入,退出时从中端滑倒上端退出。
二·PopupWindow的使用方法:
1. PopupWindow的构造方法:
这是PopupWindow的第一个构造方法,传入上下文Context。API中这个构造方法里写的是this(context,null);为什么与会有一个null,那是因为这个构造方法,实质上是调用了
![](https://img-blog.csdn.net/20170916103344680)
从这个构造方法来看,他还是调用了本类中的其他构造方法。在往后追溯呢,实质上俩个参数的构造方法又调用
了三个参数的构造方法。该方法,我就不传图片了,我直接上API里的代码:
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); }其实从这个也能看出,它是调用了四个参数的构造方法,最后发现,其实四个参数的构造方法,才是真正的构造
方法:
public PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { mContext = context; mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); final TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.PopupWindow, defStyleAttr, defStyleRes); final Drawable bg = a.getDrawable(R.styleable.PopupWindow_popupBackground); mElevation = a.getDimension(R.styleable.PopupWindow_popupElevation, 0); mOverlapAnchor = a.getBoolean(R.styleable.PopupWindow_overlapAnchor, false); // Preserve default behavior from Gingerbread. If the animation is // undefined or explicitly specifies the Gingerbread animation style, // use a sentinel value. if (a.hasValueOrEmpty(R.styleable.PopupWindow_popupAnimationStyle)) { final int animStyle = a.getResourceId(R.styleable.PopupWindow_popupAnimationStyle, 0); if (animStyle == R.style.Animation_PopupWindow) { mAnimationStyle = ANIMATION_STYLE_DEFAULT; } else { mAnimationStyle = animStyle; } } else { mAnimationStyle = ANIMATION_STYLE_DEFAULT; } final Transition enterTransition = getTransition(a.getResourceId( R.styleable.PopupWindow_popupEnterTransition, 0)); final Transition