集中展示一个PopupWindow [英] Show a PopupWindow centralized

查看:114
本文介绍了集中展示一个PopupWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的应用程序的一些弹出窗口,它的全屏及以下code:

  content.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    content.measure(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    INT屏幕宽度= windowManager.getDefaultDisplay()的getWidth()。
    INT screenHeight = windowManager.getDefaultDisplay()的getHeight()。
    INT X =屏幕宽度/ 2  -  content.getMeasuredWidth()/ 2;
    INT Y = screenHeight / 2  -  content.getMeasuredHeight()/ 2;
    window.showAtLocation(内容,Gravity.NO_GRAVITY,X,Y);
 

请在窗口展示中心。

不过,我还有一个活动是不是全屏,在弹出的打开它从它的需要是正确的下降。

试图找出为什么出现这种情况,我认为showAtLocation表明它是相对于当前的活动,但我需要证明它相对于显示。

我怎么能这样做?或者还有更简单的方法,只是弹出只是居中?

解决方案

  popupWindow.showAtLocation(anyViewOnlyNeededForWindowToken,Gravity.CENTER,0,0);
 

这将围绕你的看法。

我花了2小时痛苦摸不着头脑。我不需要任何魔法数学来处理这个问题。

视图只需要为窗口令牌,其对位置没有其他的影响

重力告诉布局管理器从哪里开始的坐标系和如何对待这些坐标。我找不到文档,但黑客正显示出我说:

  • CENTER使用弹出窗口的中间对齐到X,Y指定。所以,0,0是屏幕中心
  • BOTTOM使用弹出的底部对齐在x,y规定。所以,0,0具有弹出式底部与屏幕底部对齐。如果你想10px的填充,则Y = 10(不-10)移动弹出了屏幕10像素。

我在这里写了这件事安卓PopupWindow.showAtLocation()和重力效果

I have some popups on my app, it's fullscreen and the following code:

    content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    content.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    int screenWidth = windowManager.getDefaultDisplay().getWidth();
    int screenHeight = windowManager.getDefaultDisplay().getHeight();
    int x = screenWidth / 2 - content.getMeasuredWidth() / 2;
    int y = screenHeight / 2 - content.getMeasuredHeight() / 2;
    window.showAtLocation(content, Gravity.NO_GRAVITY, x, y);

Make the window show centered.

But I have another Activity which is not fullscreen, when the popup opens it's right-down from where it's need to be.

Trying to figure out why this happens, I think the showAtLocation shows it's relative to the current Activity, but I need to show it relative to the display.

How can I do that? Or there's a simpler way to just the popup just centered?

解决方案

popupWindow.showAtLocation(anyViewOnlyNeededForWindowToken, Gravity.CENTER, 0, 0);

This will center your view.

It took me 2 hours of pain to figure this out. I didn't need any black magic maths to handle this.

The view is only needed for the window token, it has no other impact on the location.

Gravity tells the layout manager where to start the coordinate system and how to treat those coordinates. I can't find the docs but hacking is showing me that:

  • CENTER uses the middle of the popup to be aligned to the x,y specified. So 0,0 is screen centered
  • BOTTOM uses the bottom of the popup to be aligned to the x,y specified. So 0,0 has the popup bottom aligned with the screen bottom. If you want 10px padding then y=10 (not -10) to move the popup up the screen 10 pixels.

I wrote this up over here Android PopupWindow.showAtLocation() and effects of Gravity

这篇关于集中展示一个PopupWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆