「暂停维护」PopupWindow Wrapper. 对 PopupWindow 的封装。可指定相对于 anchor view 各个方位弹出,设置背景变暗,指定 ViewGroup 背景变暗等特性。
对 PopupWindow 的封装,使得在项目中使用起来更加简单、方便、快捷
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
//...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.zyyoona7:EasyPopup:VERSION_CODE'
}
最新的VERSION_CODE
创建 EasyPopup 对象
可以调用 setXxx() 方法进行属性设置,最后调用 createPopup() 方法实现对PopupWindow的初始化。
private EasyPopup mCirclePop;
mCirclePop = EasyPopup.create()
.setContentView(this, R.layout.layout_circle_comment)
.setAnimationStyle(R.style.RightPopAnim)
//是否允许点击PopupWindow之外的地方消失
.setFocusAndOutsideEnable(true)
.apply();
初始化 View
可以调用 findViewById() 方法来获取 View 对象。
TextView tvZan=mCirclePop.findViewById(R.id.tv_zan);
TextView tvComment=mCirclePop.findViewById(R.id.tv_comment);
tvZan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showShort("赞");
mCirclePop.dismiss();
}
});
tvComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtils.showShort("评论");
mCirclePop.dismiss();
}
});
显示
相对于 view 位置显示
/**
* 相对anchor view显示,适用 宽高不为match_parent
*
* @param anchor
* @param yGravity 垂直方向的对齐方式
* @param xGravity 水平方向的对齐方式
* @param x 水平方向的偏移
* @param y 垂直方向的偏移
*/
mCirclePop.showAtAnchorView(view, YGravity.CENTER, XGravity.LEFT, 0, 0);
除了 showAtAnchorView() 方法,内部还保留了 showAsDropDown()、showAtLocation() 方法。
注意:如果使用 YGravity 和 XGravity 时,请确保使用之后 PopupWindow 没有超出屏幕边界,如果超出屏幕边界,YGravity 和 XGravity 可能无效,从而达不到你想要的效果。#4
方位注解介绍
垂直方向对齐:YGravity
YGravity.CENTER,//垂直居中
YGravity.ABOVE,//anchor view之上
YGravity.BELOW,//anchor view之下
YGravity.ALIGN_TOP,//与anchor view顶部对齐
YGravity.ALIGN_BOTTOM,//anchor view底部对齐
水平方向对齐:XGravity
XGravity.CENTER,//水平居中
XGravity.LEFT,//anchor view左侧
XGravity.RIGHT,//anchor view右侧
XGravity.ALIGN_LEFT,//与anchor view左边对齐
XGravity.ALIGN_RIGHT,//与anchor view右边对齐
mCirclePop = EasyPopup.create()
.setContentView(this, R.layout.layout_circle_comment)
.setAnimationStyle(R.style.RightPopAnim)
//是否允许点击PopupWindow之外的地方消失
.setFocusAndOutsideEnable(true)
//允许背景变暗
.setBackgroundDimEnable(true)
//变暗的透明度(0-1),0为完全透明
.setDimValue(0.4f)
//变暗的背景颜色
.setDimColor(Color.YELLOW)
//指定任意 ViewGroup 背景变暗
.setDimView(viewGroup)
.apply();
备注:背景变暗效果只支持 4.2 以上的版本。
mCirclePop = EasyPopup.create()
.setContentView(this, R.layout.layout_circle_comment)
.setAnimationStyle(R.style.RightPopAnim)
//是否允许点击PopupWindow之外的地方消失,
//设置为false点击之外的地方不会消失,但是会响应返回按钮事件
.setFocusAndOutsideEnable(false)
.apply();
EasyPopup中自定义了三个生命周期:
自定义 PopupWindow 需继承 BasePopup 抽象类,实现内部的两个抽象方法:
示例
public class ComplexPopup extends BasePopup<ComplexPopup> {
private static final String TAG = "ComplexPopup";
private Button mOkBtn;
private Button mCancelBtn;
public static ComplexPopup create(Context context){
return new ComplexPopup(context);
}
protected ComplexPopup(Context context) {
setContext(context);
}
@Override
protected void initAttributes() {
setContentView(R.layout.layout_complex, ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(300));
setFocusAndOutsideEnable(false)
.setBackgroundDimEnable(true)
.setDimValue(0.5f);
//setXxx() 方法
}
@Override
protected void initViews(View view) {
mOkBtn = findViewById(R.id.btn_ok);
mCancelBtn = findViewById(R.id.btn_cancel);
mOkBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
mCancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
mComplexPopup = ComplexPopup.create(this)
.setDimView(mComplexBgDimView)
.createPopup();
方法名 | 作用 | 备注 |
---|---|---|
setContentView(View contentView) | 设置 contentView | |
setContentView(Context context, @LayoutRes int layoutId) | 设置 contentView | |
setWidth(int width) | 设置宽 | |
setHeight(int height) | 设置高 | |
setAnchorView(View view) | 设置目标 view | |
setYGravity(@YGravity int yGravity) | 设置垂直方向对齐 | |
setXGravity(@XGravity int xGravity) | 设置水平方向对齐 | |
setOffsetX(int offsetX) | 设置水平偏移 | |
setOffsetY(int offsetY) | 设置垂直 | |
setAnimationStyle(@StyleRes int animationStyle) | 设置动画风格 | |
getContentView() | 获取PopupWindow中加载的view | @Nullable |
getContext() | 获取context | @Nullable |
getPopupWindow() | 获取PopupWindow对象 | @Nullable |
dismiss() | 消失 |
在最新的 1.1.0 版本中对代码结构进行了跳转,在之前的基础上优化了泛型的继承,使得链式调用更加的顺畅;另外对 EasyPopup 继承使用也做了优化;对部分方法的命名也做了调整。
i.继承使用修改、命名修改
ii.其他用法调整
RelativePopupWindow
CustomPopwindow
android-simple-tooltip
EasyDialog
Android弹窗_PopupWindow详解
Copyright 2017 zyyoona7
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.