将Android对话框锚定到视图

use*_*543 15 android dialog

我想知道是否可以将对话框锚定到Android中的视图,或者一般情况下:只是在屏幕上的某个位置显示对话框.

PS我不想使用PopupMenu,因为我的理解是无法自定义菜单中显示的项目 - 我最终试图在文本旁边放置一个图像以提醒用户他们有一个消息或新的东西在这里看到.

谢谢你的时间-

小智 0

Use Window params.

 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);
Run Code Online (Sandbox Code Playgroud)

I used Gravity.BOTTOM and view height to anchor dialog on top of anchor view.

Gravity.TOP will make y to apply from top and vise versa.

I used this code on onActivityCreated().