private void showPopupWindow() { View mPopView = LayoutInflater.from(MyShopItemActivity.this).inflate(R.layout.shop_find_pop, null); PopupWindow mPopWindow = new PopupWindow(mPopView,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); mPopView.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); int measuredWidth = mPopView.getMeasuredWidth(); int measuredHeight = mPopView.getMeasuredHeight(); int[] location = new int[2]; mBtnFind.getLocationOnScreen(location); //有虛擬按鍵時添加 mPopWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED); mPopWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); mPopWindow.setBackgroundDrawable(new BitmapDrawable()); mPopWindow.setFocusable(true); // 設置點擊其餘地方 就消失 (只設置這個,沒有效果) 必須設置背景 mPopWindow.setOutsideTouchable(true); mPopWindow.setTouchable(true); //在控件上方顯示 mPopWindow.showAtLocation(mBtnFind, Gravity.NO_GRAVITY, (location[0]+mBtnFind.getWidth()/2) - measuredWidth / 2, location[1] - measuredHeight); if (mPopWindow.isShowing()) { WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 0.5f; getWindow().setAttributes(params); } mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 1f; getWindow().setAttributes(params); } }); }