简体   繁体   English

将 Android 对话框锚定到视图

[英]Anchor Android Dialog to a View

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.我想知道是否可以将对话框锚定到 Android 中的视图,或者一般来说:只是在屏幕上的某个位置显示对话框。

PS 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.附注:我不想使用 PopupMenu,因为据我所知,人们无法自定义菜单中显示的项目——我最终尝试使用文本并在其旁边放置一张图片,以提醒用户他们有一个消息或新内容可在此处查看。

Thanks for your time-谢谢你的时间-

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

I used Gravity.BOTTOM and view height to anchor dialog on top of anchor view.我使用 Gravity.BOTTOM 和视图高度将对话框锚定在锚视图之上。

Gravity.TOP will make y to apply from top and vise versa. Gravity.TOP 将使 y 从顶部应用,反之亦然。

I used this code on onActivityCreated().我在 onActivityCreated() 上使用了这段代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM