- PopupWindow使用
PopupWindow这个类来实现弹出框,可以使用任意布局,这个弹出框是悬浮在activity上的。
使用如下:
//region popupwindow process
private PopupWindow pw;
private void showPopupWindow(){
View view = LayoutInflater.from(act).inflate(R.layout.layout_rental_information,null);
TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
pw = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
pw.setFocusable(true);
//不设置背景,无论点击外部区域还是back键都无法dismiss
pw.setBackgroundDrawable(new ColorDrawable());
pw.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
pw.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//显示popupWindow pw.showAtLocation(act.getWindow().getDecorView(), 0, 0, 0);
pw.showAsDropDown(waveView,0,0, Gravity.CENTER);
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismissPw();
}
});
}
private void dismissPw(){
if (null != pw && pw.isShowing()){
pw.dismiss();
}
}
//endregion
- PopupWindow源码分析
PopupWindow显示方法分为2种:
//指定位置
public void showAtLocation(View parent, int gravity, int x, int y);
//选择一个参照物View
public void showAsDropDown(View anchor);
public void showAsDropDown(View anchor, int xoff, int yoff);
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity);