点击调用showAsDropDown(view)后,PopupWindow 填充了整个屏幕,如图所示
解决后正常显示,如图2所示
解决办法:
-
计算PopupWindow要显示的高度
popupwindow的高度= LinearLayout的高度 - 控件的bottom的高度
void showOrderItemsPPWindow() { final View contentView = LayoutInflater.from(DeliveOrderActivity.this).inflate(R.layout.ppw_order_pick_type, null, false); // popupwindow的高度 = LinearLayout的高度 - 控件的bottom的高度 int height = deliveLlayout.getHeight() - orderItemBtn.getBottom(); final PopupWindow window = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, height); window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); window.setOutsideTouchable(true); window.setTouchable(true); window.showAsDropDown(orderItemBtn, 0, 0, Gravity.BOTTOM); window.setBackgroundDrawable(new ColorDrawable(Color.BLACK)); LinearLayout linearLayout = contentView.findViewById(R.id.full_llayout); linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { window.dismiss(); } }); window.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { } }); }
-
重写Popupwindow的showAsDropDown方法
public class MPopupWindow extends PopupWindow { public MPopupWindow(View contentView, int width, int height) { super(contentView, width, height); } @Override public void showAsDropDown(View anchor) { if (Build.VERSION.SDK_INT >= 24){ Rect ew = new Rect(); anchor.getGlobalVisibleRect(ew); int height = anchor.getResources().getDisplayMetrics().heightPixels - ew.top; setHeight(height); } super.showAsDropDown(anchor); } }
void showOrderItemsPPWindow() { final View contentView = LayoutInflater.from(DeliveOrderActivity.this).inflate(R.layout.ppw_order_pick_type, null, false); final MPopupWindow window = new MPopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); window.setOutsideTouchable(true); window.setTouchable(true); window.showAsDropDown(orderItemBtn); window.setBackgroundDrawable(new ColorDrawable(Color.BLACK)); LinearLayout linearLayout = contentView.findViewById(R.id.full_llayout); linearLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { window.dismiss(); } }); window.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { } }); }