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);
}