每当用户按下imageView(当前图像)时,我需要显示一个弹出窗口。
如下图所示,此弹出窗口必须与imageView左对齐。
该怎么做?
谢谢。
// Before popup , need to get PopupWindow size.
// because contentView is not drawn in this time, width/height is 0.
// so, need to by measure to get contentView's size.
private static int makeDropDownMeasureSpec(int measureSpec) {
int mode;
if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
mode = View.MeasureSpec.UNSPECIFIED;
} else {
mode = View.MeasureSpec.EXACTLY;
}
return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
}
// measure contentView size
View contentView = popupWindow.getContentView();
// need to measure first, because in this time PopupWindow is nit pop, width is 0.
contentView.measure(makeDropDownMeasureSpec(popupWindow.getWidth()),
makeDropDownMeasureSpec(popupWindow.getHeight()));
int offsetX = -popupWindow.getContentView().getMeasuredWidth();
int offsetY = -mTriggerView.getHeight();
// show at the left edge of the trigger view
popupWindow.showAsDropDown(mTriggerView, offsetX, offsetY);
尝试这行代码:
popupWindow.showAtLocation(image,Gravity.LEFT, 0, 0);
希望能有所帮助!
popupWindow
。 - William