自定义一个Popwindow
public class PopWindow1 extends PopupWindow {
private WindowManager.LayoutParams params;
private Activity context;
public static final int SHOWDROPDOWN = 1;
public static final int SHOWATLOCATION = 0;
private PopupWindow popupWindow;
public PopWindow1(final Activity content, int popLayoutId) {
this.context = content;
params = content.getWindow().getAttributes();
View contentView = LayoutInflater.from(content).inflate(popLayoutId, null, true);
popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.GRAY));
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
params.alpha = 1.0f;
context.getWindow().setAttributes(params);
dismiss();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void showPopWindow(View view, int type, int offX, int offY, int gravity) {
params.alpha = 0.7f;
context.getWindow().setAttributes(params);
if (type == SHOWATLOCATION) {
popupWindow.showAtLocation(view, gravity, offX, offY);
} else if (type == SHOWDROPDOWN) {
popupWindow.showAsDropDown(view, offX, offY, gravity);
}
}
}
在Activity中调用
public class PopwindowActivity extends BaseActivity {
@Bind(R.id.popwindow1)
Button popwindow1;
private PopWindow1 pw;
private View contentView;
private WindowManager.LayoutParams params;
@Override
protected int getLayoutid() {
return R.layout.activity_popwindow;
}
@Override
protected void setUpView() {
super.setUpView();
pw = new PopWindow1(this, R.layout.popwindow_item1);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@OnClick(R.id.popwindow1)
public void onViewClicked() {
pw.showPopWindow(popwindow1, PopWindow1.SHOWDROPDOWN, 0, 0, Gravity.BOTTOM);
}
}