PopupWindow

輸入圖片說明

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();
		}
	}
}
相關文章
相關標籤/搜索