您的位置:首页 > 运维架构

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

2016-05-25 16:20 387 查看
/** 相对于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 否则获取宽度可能有问题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: