700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android PupopWindow适配全面屏 全屏显示覆盖状态栏 简单实用只需两步

Android PupopWindow适配全面屏 全屏显示覆盖状态栏 简单实用只需两步

时间:2018-07-20 11:46:58

相关推荐

Android PupopWindow适配全面屏  全屏显示覆盖状态栏 简单实用只需两步

开发中有一个需求弹出一个弹窗广告,背景需要半透明全屏显示,覆盖状态栏(见下图)。我选用了PupopWindow实现。

然后设置了属性宽高为WindowManager.LayoutParams.MATCH_PARENT

结果可想而知,根本无法全屏显示,状态栏部分无法遮盖。看网上有很多方法,都说需要自定义PopupWindow,我觉得实现一个全屏显示就需要自定义太麻烦了。就开始找查资料,找方法。

最后得出了一个两步解决问题的方法:

第一步:获取屏幕的高,设置popupWindow的高为屏幕的高+顶部状态栏的高+底部虚拟按键的高

int height = getResources().getDisplayMetrics().heightPixels;// 屏幕的高actionPopupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, height + getStatusBarHeight(mContext) + getBottomKeyboardHeight(mContext));//设置popupWindow的高为屏幕的高+顶部状态栏的高+底部虚拟按键的高

/*** 获取状态栏高度** @param context context* @return 状态栏高度*/public static int getStatusBarHeight(Context context) {// 获得状态栏高度int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");return context.getResources().getDimensionPixelSize(resourceId);}/*** 获取底部虚拟键盘的高度*/public static int getBottomKeyboardHeight(Activity context) {int screenHeight = getAccurateScreenDpi(context)[1];DisplayMetrics dm = new DisplayMetrics();context.getWindowManager().getDefaultDisplay().getMetrics(dm);int heightDifference = screenHeight - dm.heightPixels;return heightDifference;}

第二步:设置popupWindow属性setClippingEnabled(false);

actionPopupWindow.setClippingEnabled(false);

再运行试下,是不是全屏覆盖显示了?是的。问题解决!就是这么简单2步

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。