美文网首页
Powindow真正意义上的铺满全屏

Powindow真正意义上的铺满全屏

作者: ana生 | 来源:发表于2018-12-20 16:55 被阅读0次

新建一个类extends PopupWindow,在setContentView之后,动态设置pop的宽高:

 /**
 * 设置PopupWindow的大小
 *
 * @param context
 */
private void calWidthAndHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);

    int mWidth = metrics.widthPixels;
    //设置高度为全屏高度的70%
    int mHeight = (int) (metrics.heightPixels);
//        int mHeight = (int) (metrics.heightPixels * 0.7);
    setWidth(mWidth);
    setHeight(mHeight);
    setClippingEnabled(false);

}

运行。。。。。。
发现有点美中不足,状态栏没办法铺盖,于是。。。

有2种方法:

第一种:
popupWindow.setClippingEnabled(false);
第二种:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
            mLayoutInScreen.setAccessible(true);
            mLayoutInScreen.set(popupWindow, true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

完美收工!

相关文章

网友评论

      本文标题:Powindow真正意义上的铺满全屏

      本文链接:https://www.haomeiwen.com/subject/nztukqtx.html