5

I need to display a popup window whenever user presses the imageView(the present image). This popup window must be aligned to the left of the imageView as shown in the attached image. enter image description here

How It can be done? Thanks.

4

2 回答 2

2

要将 PopupWindow 对齐到触发器视图的左侧,您必须在绘制 PopupWinow 之前知道它的宽度。

// 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);
于 2018-07-24T00:40:12.300 回答
-1

试试这行代码:

popupWindow.showAtLocation(image,Gravity.LEFT, 0, 0);

希望能帮助到你!!

于 2013-08-01T10:27:48.830 回答