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

5

每当用户按下imageView(当前图像)时,我需要显示一个弹出窗口。

如下图所示,此弹出窗口必须与imageView左对齐。

enter image description here

该怎么做?

谢谢。

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);

-1

尝试这行代码:

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

希望能有所帮助!


这将在屏幕的左上角显示popupWindow - William

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接