自定义popupwindow几乎是每一个程序比不可少的一项工做,github上也有不少这样的开源库,功能非常强大,可是有时候须要修改就比较麻烦,这里提供一种简洁、比较经常使用的popupwindow供你们使用。CommonPopup代码以下git
public class CommonPopup implements PopupWindow.OnDismissListener{
private Context mContext;
private int resLayoutId = -1;
private View customerView;
private int width;
private int height;
private boolean focusable = true;//焦点
private boolean touchable = true;//是否可被点击
private boolean clippingEnable = true;//容许弹出窗口扩展到屏幕的边界以外
private boolean outsideTouchable = true;//外部是否消失
private boolean darkEnable = false;//是否背景变暗
private float darkValue = 0;//背景变暗的值,0-1
private int animationStyle = -1;//动画
private PopupWindow mPopupWindow;
private Window mWindow;
private PopupWindow.OnDismissListener mOnDismissListener;
private static final float DEFAULT_ALPHA = 0.7f;
private CommonPopup(Context context){
mContext = context;
}
private PopupWindow build(){
setCustomerView();
setConfig();
setOutsideTouchable();
setDark();
setAnimationStyle();
mPopupWindow.update();
return mPopupWindow;
}
/**
* 设置布局
*/
private void setCustomerView(){
if(customerView == null){
customerView = LayoutInflater.from(mContext).inflate(resLayoutId,null);
}
if(width != 0 && height != 0 ){
mPopupWindow = new PopupWindow(customerView,width,height);
}else{
mPopupWindow = new PopupWindow(customerView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
if(width == 0 || height == 0){
mPopupWindow.getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
width = mPopupWindow.getContentView().getMeasuredWidth();
height = mPopupWindow.getContentView().getMeasuredHeight();
}
}
/**
* 设置基本配置属性
*/
private void setConfig(){
mPopupWindow.setTouchable(touchable);
mPopupWindow.setFocusable(focusable);
mPopupWindow.setClippingEnabled(clippingEnable);
if(mOnDismissListener != null){
mPopupWindow.setOnDismissListener(mOnDismissListener);
}
mPopupWindow.setOnDismissListener(this);
}
/**
* 设置点击外部监听,兼容6.0 outsidetorchable 失效问题
*/
private void setOutsideTouchable(){
if(outsideTouchable){
mPopupWindow.setFocusable(focusable);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setOutsideTouchable(outsideTouchable);
}else{
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(outsideTouchable);
mPopupWindow.setBackgroundDrawable(null);
mPopupWindow.getContentView().setFocusable(true);
mPopupWindow.getContentView().setFocusableInTouchMode(true);
mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mPopupWindow.dismiss();
return true;
}
return false;
}
});
mPopupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
if ((event.getAction() == MotionEvent.ACTION_DOWN)
&& ((x < 0) || (x >= width) || (y < 0) || (y >= height))) {
return true;
} else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
return true;
}
return false;
}
});
}
}
/**
* 设置背景
*/
private void setDark(){
if(mContext != null && darkEnable){
float alpha = (darkValue > 0 && darkValue < 1) ? darkValue : DEFAULT_ALPHA;
mWindow = ((Activity) mContext).getWindow();
WindowManager.LayoutParams params = mWindow.getAttributes();
params.alpha = alpha;
mWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
mWindow.setAttributes(params);
}
}
/**
* 设置动画
*/
private void setAnimationStyle(){
if(animationStyle != -1){
mPopupWindow.setAnimationStyle(animationStyle);
}
}
/**
* 显示
* @param anchor
* @return
*/
public CommonPopup showAsDropDown(View anchor){
if(mPopupWindow != null){
mPopupWindow.showAsDropDown(anchor);
}
return this;
}
/**
* 显示
* @param anchor
* @param xOff 水平
* @param yOff 竖直
* @return
*/
public CommonPopup showAsDropDown(View anchor, int xOff, int yOff){
if(mPopupWindow != null){
mPopupWindow.showAsDropDown(anchor,xOff,yOff);
}
return this;
}
/**
* 显示
* @param anchor
* @param xOff 水平
* @param yOff 竖直
* @param gravity 重心
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public CommonPopup showAsDropDown(View anchor, int xOff, int yOff, int gravity){
if(mPopupWindow != null){
mPopupWindow.showAsDropDown(anchor,xOff,yOff,gravity);
}
return this;
}
/**
* 显示
* @param parent
* @param gravity
* @param x
* @param y
* @return
*/
public CommonPopup showAtLocation(View parent, int gravity, int x, int y){
if(mPopupWindow != null){
mPopupWindow.showAtLocation(parent,gravity,x,y);
}
return this;
}
@Override
public void onDismiss() {
dismiss();
}
/**
* 关闭
*/
public void dismiss(){
if(mOnDismissListener != null){
mOnDismissListener.onDismiss();
}
if(mWindow != null){
WindowManager.LayoutParams params = mWindow.getAttributes();
params.alpha = 1.0f;
mWindow.setAttributes(params);
}
if(mPopupWindow != null && mPopupWindow.isShowing()){
mPopupWindow.dismiss();
}
}
public static class Builder{
private CommonPopup commonPopup;
public Builder(Context context){
commonPopup = new CommonPopup(context);
}
public Builder setView(@LayoutRes int resLayoutId){
commonPopup.resLayoutId = resLayoutId;
commonPopup.customerView = null;
return this;
}
public Builder setView(View view){
commonPopup.customerView = view;
commonPopup.resLayoutId = -1;
return this;
}
public Builder setSize(int width,int height){
commonPopup.width = width;
commonPopup.height = height;
return this;
}
public Builder setFocusable(boolean focusable){
commonPopup.focusable = focusable;
return this;
}
public Builder setTouchable(boolean touchable){
commonPopup.touchable = touchable;
return this;
}
public Builder setClippingEnable(boolean clippingEnable){
commonPopup.clippingEnable = clippingEnable;
return this;
}
public Builder setOutsideTouchable(boolean outsideTouchable){
commonPopup.outsideTouchable = outsideTouchable;
return this;
}
public Builder setDarkEable(boolean darkEnable){
commonPopup.darkEnable = darkEnable;
return this;
}
public Builder setDarkValue(float darkValue){
commonPopup.darkValue = darkValue;
return this;
}
public Builder setAnimationStyle(int animationStyle){
commonPopup.animationStyle = animationStyle;
return this;
}
public Builder setOnDissmissListener(PopupWindow.OnDismissListener onDismissListener){
commonPopup.mOnDismissListener = onDismissListener;
return this;
}
public CommonPopup create(){
commonPopup.build();
return commonPopup;
}
}
}
复制代码
CommonPopup 使用方法以下github
new CommonPopup.Builder(MainActivity.this)
.setView(customerView)
.setSize(ScreenUtils.dp2px(200),ScreenUtils.dp2px(100))
.setFocusable(true)
.setTouchable(true)
.setClippingEnable(true)
.setOutsideTouchable(false)
.setDarkEable(true)
.setDarkValue(0.5f)
.create()
.showAsDropDown(tvRead,50,-200);
复制代码