pinguo-zhouwei / CustomPopwindow

对PopupWindow常用API的简单封装,几行代码就搞定PopupWindow弹窗,使用Builder模式,链式调用,像使用AlertDialog 一样
1.42k stars 291 forks source link

如何让popwindow的上部不变暗,下部变暗 #27

Open CharlesWWT opened 6 years ago

CharlesWWT commented 6 years ago

比如我在中间弹出的popwindow,上下都变暗了,我只想让下部变暗 _20180426152153

yecaler commented 6 years ago

如果只想使用style设置,是没有办法的。 所以只能你写的时候给布局设置长一点,底部设置成你想要的透明度。也就是变暗。上面在style里面设置不变暗。

CQBOBOZHU commented 6 years ago

你可以加载的布局里面最下方添加一个半透明的view。但是这个方法在华为手机上会造成全屏效果。

CQBOBOZHU commented 6 years ago

public static void show(final PopupWindow popupWindow, final View view) { //PopupWindowCompat.showAsDropDown(popupWindow, view, 0, 0, Gravity.NO_GRAVITY); if (Build.VERSION.SDK_INT < 24) { popupWindow.showAsDropDown(view); } else if (Build.VERSION.SDK_INT == 24) { // 适配 android 7.0 int[] location = new int[2]; view.getLocationOnScreen(location); int x = location[0]; int y = location[1]; popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, y + view.getHeight()); } else if (Build.VERSION.SDK_INT > 24) { // 适配 android 7.1.1 int[] location = new int[2]; view.getLocationOnScreen(location); int x = location[0]; final int offsetY = location[1] + view.getHeight(); Rect r = new Rect(); view.getRootView().getWindowVisibleDisplayFrame(r); int screenHeight = (r.bottom - r.top + ScreenUtils.getStatusHeight(view.getContext())); popupWindow.setHeight(screenHeight - offsetY); popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, offsetY); view.getRootView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); view.getRootView().getWindowVisibleDisplayFrame(r); int screenHeight = (r.bottom - r.top + ScreenUtils.getStatusHeight(view.getContext())); popupWindow.setHeight(screenHeight - offsetY); popupWindow.update(0, offsetY, r.right - r.left, screenHeight - offsetY); } }); } } 上面全面效果解决办法。 用这个。