PopupWindow居中显示,位置大小自定义

1.位于父控件下方

 View view=View.inflate(getContext(),R.layout.popupwod_item,null);
        TextView pick=view.findViewById(R.id.pick);
        TextView camera=view.findViewById(R.id.camera);
        final TextView quxiao=view.findViewById(R.id.quxiao);
        window=new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);

        //点击外部,pp直接隐藏  两部缺一不可
        window.setTouchable(true);
        window.setBackgroundDrawable(new BitmapDrawable());


        //设置距离
        window.showAsDropDown(tx_icon,90,30);

2.位于父控件下方 

   View view=View.inflate(this,R.layout.pop,null);

        popupWindow=new PopupWindow(view,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setContentView(view);
        TextView t1=view.findViewById(R.id.re);
        TextView t2=view.findViewById(R.id.zz);
        final TextView quxiao=view.findViewById(R.id.quxiao);
        View view1=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
        popupWindow.showAsDropDown(pop);

3.实用,在页面居中显示,大小可控制

 //获取布局
                final View inflate = View.inflate(MyPurseActivity.this, R.layout.pop_money_item, null);
                TextView text_zfb=inflate.findViewById(R.id.text_zfb);
                //这一步是为了获取屏幕的宽高,便于用来设置PopupWindow的大小
                DisplayMetrics dm = getResources().getDisplayMetrics();
                /*创建pop(视图,宽,高)*/
                popupWindow =  new PopupWindow(inflate,dm.widthPixels/11*5,dm.heightPixels/11*4);
                //   popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); 
                          popupWindow.setOutsideTouchable (false);//外部不可点击
                popupWindow.setFocusable(false);
  popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_money_zfb));
                //展示pop
                popupWindow.showAtLocation(MyPurseActivity.this.getWindow().getDecorView(),Gravity.CENTER,0,0);

                text_zfb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                }
2.重写
  @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(popupWindow!=null&&popupWindow.isShowing()){
            return false;
        }
        return super.dispatchTouchEvent(ev);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值