CustomerPopWindow popWindow = new CustomerPopWindow(mContext); popWindow.showPopup(toolbar); //类 public class CustomerPopWindow extends PopupWindow { private static final String TAG = "CustomerPopWindow"; public CustomerPopWindow(final Context context){ LayoutInflater layoutInflater = LayoutInflater.from(context); View popView = layoutInflater.inflate(R.layout.customer_popup, null, false); // 设置SelectPicPopupWindow的View this.setContentView(popView); this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); this.setOutsideTouchable(true); // 点击“返回Back”也能使其消失,而且并不会影响你的背景 //this.setBackgroundDrawable(new BitmapDrawable(context.getResources(), // Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888))); this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); this.update(); TextView tvAllCustomer = (TextView) popView.findViewById(R.id.tv_all_customer); TextView tvWechatCustomer = (TextView) popView.findViewById(R.id.tv_wechat_customer); TextView tvOtherCustomer = (TextView) popView.findViewById(R.id.tv_other_customer); tvAllCustomer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "==tvAllCustomer=="); CustomerPopWindow.this.dismiss(); } }); tvWechatCustomer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "==tvWechatCustomer=="); CustomerPopWindow.this.dismiss(); } }); tvOtherCustomer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "==tvOtherCustomer=="); CustomerPopWindow.this.dismiss(); } }); } public void showPopup(View parent) { if(!this.isShowing()){ //int[] location = new int[2]; //parent.getLocationOnScreen(location); //this.showAtLocation(parent, Gravity.TOP|Gravity.CENTER_HORIZONTAL, // location[0]-this.getWidth(), location[1]); this.getContentView().measure(0,0); //测量ContentView尺寸 int xPos = (parent.getWidth() - this.getContentView().getMeasuredWidth()) / 2; //获取偏移量 this.showAsDropDown(parent, xPos, 0); }else{ this.dismiss(); } } }