17

I wanted to know if it is possible to anchor a Dialog to a View in Android, or in general: just to display the dialog at a certain location on the screen.

P.S. I don't want to use a PopupMenu because it is my understanding that one cannot customize the items displayed in the menu-- I'm ultimately trying to have text and put an image next to it to alert the user that they have a message or something new to see here.

Thanks for your time-

4

1 回答 1

0

使用窗口参数。

 WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.copyFrom(getDialog().getWindow().getAttributes());
    params.gravity = Gravity.BOTTOM;
    params.y = YOUR_ANCHOR_VIEW.getHeight();
    getDialog().getWindow().setAttributes(params);

我使用 Gravity.BOTTOM 和视图高度将对话框锚定在锚视图之上。

Gravity.TOP 将使 y 从顶部应用,反之亦然。

我在 onActivityCreated() 上使用了这段代码。

于 2019-11-15T02:24:22.280 回答