Dialog 显示在指定位置,类似于popwindow的效果

先看一下效果,点击右上角的钟表,弹出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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值