先看一下效果,点击右上角的钟表,弹出dialog窗口,和popwindow基本上一样,但是popwindow在6.0以后的版本中,
PopupWindow.showAsDropDown()方法的缺陷大家应该遇见过:就是在创建popwindow的时候,如果高度设置match_parent的话,会失效。
<!--dialog实现popwindow的样式-->
<style name="dialog_popwindow">
<!--对话框背景 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!--边框 -->
<item name="android:windowFrame">@null</item>
<!--没有标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮现在Activity之上 -->
<item name="android:windowIsFloating">true</item>
<!--背景透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 是否有覆盖 -->
<item name="android:windowContentOverlay">@null</item>
<!--进出的显示动画 -->
<item name="android:windowAnimationStyle">@style/normalDialogAnim</item>
<!--背景变暗-->
<item name="android:backgroundDimEnabled">false</item>
</style>
Dialog dialog1 = new Dialog(this, R.style.dialog_popwindow);
View inflate = LayoutInflater.from(this).inflate(R.layout.time_pop, null);
//弹窗点击周围空白处弹出层自动消失弹窗消失(false时为点击周围空白处弹出层不自动消失)
dialog1.setCanceledOnTouchOutside(true);
//将布局设置给Dialog
dialog1.setContentView(inflate);
//获取当前Activity所在的窗体
Window window = dialog1.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
//获取通知栏高度 重要的在这,获取到通知栏高度
int notificationBar = Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
//获取控件 textview 的绝对坐标,( y 轴坐标是控件上部到屏幕最顶部(不包括控件本身))
//location [0] 为x绝对坐标;location [1] 为y绝对坐标
int[] location = new int[2];
title_view.getLocationInWindow(location); //获取在当前窗体内的绝对坐标
title_view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
wlp.gravity = Gravity.TOP;
// wlp.x=100; //对 dialog 设置 x 轴坐标
wlp.y = location[1] + title_view.getHeight() - notificationBar; //对dialog设置y轴坐标
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
wlp.height = (ScreenSizeTool.getInstance(this).getScreenHeight()) - (title_view.getHeight() + notificationBar);
window.setAttributes(wlp);
dialog1.show();//显示对话框
TextView createDateFrom = inflate.findViewById(R.id.createDateFrom);
TextView createDateTo = inflate.findViewById(R.id.createDateTo);
Button query = inflate.findViewById(R.id.query);
感谢作者:https://blog.csdn.net/wuqingsen1/article/details/80096469