本文实例为大家分享了Android PopupWindow增加半透明蒙层的具体代码,供大家参考,具体内容如下
先看效果图:
实现代码:
BasePopupWindowWithMask.class
package com.example.popupwindowwithmask;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
/**
* Created by kk on 2017/7/22.
*/
public abstract class BasePopupWindowWithMask extends PopupWindow {
protected Context context;
private WindowManager windowManager;
private View maskView;
public BasePopupWindowWithMask(Context context) {
super(context);
this.context = context;
windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
setContentView(initContentView());
setHeight(initHeight());
setWidth(initWidth());
setOutsideTouchable(true);
setFocusable(true);
setTouchable(true);
setBackgroundDrawable(new ColorDrawable());
}
protected abstract View initContentView();
protected abstract int initHeight();
protected abstract int initWidth();
@Override
public void showAsDropDown(View anchor) {
addMask(anchor.getWindowToken());
super.showAsDropDown(anchor);
}
private void addMask(IBinder token) {
WindowManager.LayoutParams wl = new WindowManager.LayoutParams();
wl.width = WindowManager.LayoutParams.MATCH_PARENT;
wl.height = WindowManager.LayoutParams.MATCH_PARENT;
wl.format = PixelFormat.TRANSLUCENT;//不设置这个弹出框的透明遮罩显示为黑色
wl.type = WindowManager.LayoutParams.TYPE_APPLI