如何将弹出窗口对齐到视图的左侧

时间:2013-08-01 10:23:49

标签: android popup alignment

每当用户按下imageView(当前图像)时,我都需要显示一个弹出窗口。 此弹出窗口必须与imageView的左侧对齐,如附图所示。 enter image description here

如何做到这一点? 感谢。

2 个答案:

答案 0 :(得分:0)

试试这行代码:

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

希望它能帮助!!

答案 1 :(得分:0)

要将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);
相关问题