我有一个弹出窗口,可以从我的片段中的侦听器调用。在按下按钮之前它会立即显示。但是,在将创建它的方法移动到自定义侦听器后,它不会立即弹出。而是仅在屏幕上的任何位置发生单击事件或滚动事件之后。我猜测屏幕需要以某种方式刷新以显示弹出窗口?这是我的创建代码:
/***POP UP WINDOW CODE***/
PopupWindow popupMessage;
PopupWindow pw;
@SuppressLint("NewApi")
private void popupInit(){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Get screen dimensions to set the window size
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
final View layout = inflater.inflate(R.layout.add_interview, null, false);
// create a 300px width and 470px height PopupWindow
pw = new PopupWindow(layout, (int) (width*(0.8)), (int) (height*(0.8)), true);
// display the popup in the center
layout.post(new Runnable() {
public void run() {
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());
}
});
}
答案 0 :(得分:3)
如果您不想延迟弹出窗口,请执行以下操作:
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
pw.update(0, 0, pw.getWidth(), pw.getHeight());