PopupWindow弹出框(自定义弹出框)自定义位置

来源:互联网 发布:php 投票cookie 限制 编辑:程序博客网 时间:2024/05/21 12:54



调用:


         popupwindow_right = buildPopupWindow(v,
                                                    R.layout.friend_circle_notify_popup);     //加载自定义的布局
            
            popupwindow_right.findViewById(R.id.friend).setOnClickListener(this);    按钮监听



定义弹出框位置和大小(无暗色背景)

   布局设定,自适应自己的大小

public View buildPopupWindow(View v, int resource) {
        //window = new PopupWindow(ac);
        window = new PopupWindow(v, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);
        DisplayMetrics outMetrics = new DisplayMetrics();

        this.getWindowManager().getDefaultDisplay()
                .getMetrics(outMetrics);
        window.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        window.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        View view = this.getLayoutInflater().inflate(resource, null);
        window.setContentView(view);
        window.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        window.setBackgroundDrawable(dw);

        //window.showAsDropDown(v);

    //弹出框位置

        window.showAtLocation(v, Gravity.TOP|Gravity.RIGHT, 10, 150);

       

      //监听如点击别处解散弹出框

        view.setOnTouchListener(new OnTouchListener() {
            
            public boolean onTouch(View view, MotionEvent event) {
                
                int height = view.findViewById(R.id.popo_layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y>height){
                        window.dismiss();
                    }
                }                
                return true;
            }
        });
        
        return view;

    }


有暗色背景的布局

布局A要填充半透明背景




0 0
原创粉丝点击