- 概述
- 常用方法
- 基本使用
- 封装 PopupWindow
- 使用封装后的PopupWindow
- 显示效果
概述
常用设置
public PopupWindow (Context context)
public PopupWindow(View contentView)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView, int height, boolean focusable)
window.setContentView(contentView);
window.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
window.setOutsideTouchable(true);
window.setOnDismissListener(this);
window.setTouchable(true);
window.setAnimationStyle(R.style.PopupWindowTranslateTheme);
void showAtLocation (View parent, int gravity, int x, int y)
void showAsDropDown (View anchor, int xoff, int yoff, int gravity)
void showAsDropDown (View anchor, int yoff)
void showAsDropDown (View anchor)
基本使用
PopupWindow window = new PopupWindow(this);
window.setContentView(contentView);
window.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
window.setOutsideTouchable(true);
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
window.setTouchable(true);
window.setAnimationStyle(R.style.PopupWindowTranslateTheme);
window.showAtLocation(btnTarget, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
封装 PopupWindow
mPopupWindow.getContentView().measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int popupWidth = mPopupWindow.getContentView().getMeasuredWidth();
int popupHeight = mPopupWindow.getContentView().getMeasuredHeight();
public Builder(Context context) {
this.context = context;
this.popupWindow = new PopupWindow(context);
this.outsideTouchable = true;
this.touchable = true;
this.backgroundDrawable = new ColorDrawable(Color.TRANSPARENT);
this.width = WindowManager.LayoutParams.WRAP_CONTENT;
this.height = WindowManager.LayoutParams.WRAP_CONTENT;
this.gravity = Gravity.CENTER;
this.layoutId = -1;
this.offsetX = 0;
this.offsetY = 0;
}
private void setPopupWindowConfig(MPopupWindow window) {
if (contentView != null && layoutId != -1){
throw new MException("setContentView and setLayoutId can't be used together.", "0");
}else if (contentView == null && layoutId == -1){
throw new MException("contentView or layoutId can't be null.", "1");
}
if (context == null) {
throw new MException("context can't be null.", "2");
} else {
window.mContext = this.context;
}
window.mWidth = this.width;
window.mHeight = this.height;
window.mView = this.contentView;
window.mLayoutId = layoutId;
window.mPopupWindow = this.popupWindow;
window.mOutsideTouchable = this.outsideTouchable;
window.mBackgroundDrawable = this.backgroundDrawable;
window.mOnDismissListener = this.onDismissListener;
window.mAnimationStyle = this.animationStyle;
window.mTouchable = this.touchable;
window.mOffsetX = this.offsetX;
window.mOffsetY = this.offsetY;
window.mGravity = this.gravity;
}
}
public void showPopupWindow(View v, LocationType type) {
if (mView!=null){
mPopupWindow.setContentView(mView);
}else if (mLayoutId != -1){
View contentView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
mPopupWindow.setContentView(contentView);
}
mPopupWindow.setWidth(mWidth);
mPopupWindow.setHeight(mHeight);
mPopupWindow.setBackgroundDrawable(mBackgroundDrawable);
mPopupWindow.setOutsideTouchable(mOutsideTouchable);
mPopupWindow.setOnDismissListener(mOnDismissListener);
mPopupWindow.setAnimationStyle(mAnimationStyle);
mPopupWindow.setTouchable(mTouchable);
int[] locations = new int[2];
v.getLocationOnScreen(locations);
int left = locations[0];
int top = locations[1];
mPopupWindow.getContentView().measure(
View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int popupWidth = mPopupWindow.getContentView().getMeasuredWidth();
int popupHeight = mPopupWindow.getContentView().getMeasuredHeight();
switch (type) {
case TOP_LEFT:
mPopupWindow.showAtLocation(v,Gravity.NO_GRAVITY,left - popupWidth + mOffsetX,top - popupHeight + mOffsetY);
break;
case TOP_CENTER:
int offsetX = (v.getWidth() - popupWidth) / 2;
mPopupWindow.showAtLocation(v,left + offsetX + mOffsetX,top - popupHeight + mOffsetY);
break;
case TOP_RIGHT:
mPopupWindow.showAtLocation(v,left + v.getWidth() + mOffsetX,top - popupHeight + mOffsetY);
break;
case BOTTOM_LEFT:
mPopupWindow.showAsDropDown(v, -popupWidth + mOffsetX,mOffsetY);
break;
case BOTTOM_CENTER:
int offsetX1 = (v.getWidth() - popupWidth) / 2;
mPopupWindow.showAsDropDown(v,offsetX1 + mOffsetX,mOffsetY);
break;
case BOTTOM_RIGHT:
mPopupWindow.showAsDropDown(v, v.getWidth() + mOffsetX,mOffsetY);
break;
case LEFT_TOP:
mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, left - popupWidth + mOffsetX, top - popupHeight + mOffsetY);
break;
case LEFT_BOTTOM:
mPopupWindow.showAtLocation(v, top + v.getHeight() + mOffsetY);
break;
case LEFT_CENTER:
int offsetY = (v.getHeight() - popupHeight) / 2;
mPopupWindow.showAtLocation(v,top + offsetY + mOffsetY);
break;
case RIGHT_TOP:
mPopupWindow.showAtLocation(v, left + v.getWidth() + mOffsetX,top - popupHeight + mOffsetY);
break;
case RIGHT_BOTTOM:
mPopupWindow.showAtLocation(v,top + v.getHeight() + mOffsetY);
break;
case RIGHT_CENTER:
int offsetY1 = (v.getHeight() - popupHeight) / 2;
mPopupWindow.showAtLocation(v,top + offsetY1 + mOffsetY);
break;
case FROM_BOTTOM:
mPopupWindow.showAtLocation(v,mGravity,mOffsetX,mOffsetY);
break;
}
}
使用封装后的PopupWindow
private void showPopupWindow(MPopupWindow.LocationType type) {
MPopupWindow popupWindow = new MPopupWindow
.Builder(this)
.setLayoutId(R.layout.popup_window_layout)
.build();
popupWindow.showPopupWindow(btnTarget, type);
}
显示效果: