让popupwindow显示在view的上方并与该view水平居中对齐

/** 相对于listen这个view*/

private void showPopupWindow(View listen) {

       PopupWindow popupWindow;

        View contentView = LayoutInflater.from(HomepageActivity.this).inflate(
                R.layout.realtime_order_pop, null);
        // 设置关闭按钮的点击事件
        Button button = (Button) contentView.findViewById(R.id.order_pop_close);
        button.setOnClickListener(this);//if(popupWindow != null){popupWindow.dismiss();}
        popupWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
        // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.order_pop_bg));
        popupWindow.setOutsideTouchable(false); // 如果已经.setBackgroundDrawable()则本句不起作用
popupWindow.getContentView().measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int popupWidth = popupWindow.getContentView().getMeasuredWidth();
int popupHeight = popupWindow.getContentView().getMeasuredHeight();
        // 设置好参数之后再show
        int[] location = new int[2];  
        listen.getLocationOnScreen(location);  
        popupWindow.showAtLocation(listen,  Gravity.NO_GRAVITY, (location[0]+listen.getWidth()/2)-popupWidth/2 , location[1]-popupHeight);


    }



《!注意 布局文件 最外面不要用framelayouut 否则获取宽度可能有问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值