这是我的代码:
public class CustomPopUpWindowOfAdd extends PopupWindow { private Context mContext; private PopupWindow popupWindow; private RelativeLayout anchorView; @InjectView(R.id.store_rippleView) LayoutRipple store_ripple; @InjectView(R.id.scan_rippleView) LayoutRipple scan_ripple; @InjectView(R.id.manually_rippleView) LayoutRipple manually_ripple; public CustomPopUpWindowOfAdd(Context context, RelativeLayout anchorView){ super(context); this.mContext = context; this.anchorView = anchorView; View view = LayoutInflater.from(mContext).inflate(R.layout.add_menu, null); setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.menu_shape)); setContentView(view); setFocusable(true); setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { dismiss(); return true; } return false; } }); ButterKnife.inject(this,view); initViewAction(); // popupWindow = new PopupWindow(view); } private void initViewAction(){ store_ripple.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AVAnalytics.onEvent(mContext, "openStore"); // mContext.startActivity(new Intent(mContext, WebAppStoreActivity.class)); dismiss(); } }); scan_ripple.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mContext.startActivity(new Intent(mContext, BarCodeScanActivity.class)); dismiss(); } }); manually_ripple.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mContext.startActivity(new Intent(mContext, ManuallyAddActivity.class)); dismiss(); } }); } public void show(){ LogUtil.i("show window"); int offsetX = Utils.dpToPx(-148,mContext.getResources()); int offsetY = Utils.dpToPx(-48,mContext.getResources()); showAsDropDown(anchorView, offsetX, offsetY); } }
无论我offsetX设置什么值,他显示都是靠右边屏幕
很简单,showAsDropDown显示的点是以anchorView左下角点为参照点.
改为