在Android的定位弹出窗口 [英] Positioning a Popup Window in Android

查看:91
本文介绍了在Android的定位弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图基于定位元素在屏幕上的动态定位的弹出窗口。出于某种原因,当我放置该元素看起来大约100像素的,我想它的左边。举例来说,如果我在弹出的窗口应该出现与它的右边缘正好锚右边缘左侧的屏幕边缘附近的锚定位。然而,在弹出窗口出现冲洗到屏幕的边缘。如果我减去100从我的计算XPOS然后正常工作。

我已经检查了我的数学,并通过显示code走到检查值不出来错误,所以我不知道是什么原因造成的问题。我刚学的观点在安卓的,所以我敢肯定,我忽视的东西。希望这里有人会知道!

我已显示code以下,并乐意在必要时发布任何东西。

在此先感谢!

  INT []的位置=新INT [2];
    this.anchor.getLocationOnScreen(位置);

    矩形anchorRect =
            新的矩形(位置[0],位置[1],位置[0] + this.anchor.getWidth(),位置[1]
                + this.anchor.getHeight());
    this.anchorRectangle = anchorRect;


    this.root.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    this.root.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);

    INT rootWidth = this.root.getMeasuredWidth();
    INT rootHeight = this.root.getMeasuredHeight();

     INT屏幕宽度= this.windowManager.getDefaultDisplay()的getWidth()。

    INT XPOS,yPos;
    //对齐弹出与右如果根比锚更宽
    //对准离开,否则
    如果(rootWidth> anchorRect.right  -  anchorRect.left){
        XPOS = anchorRect.right  -  rootWidth;
    } 其他 {
        XPOS = anchorRect.left + 15;
    }

    //正确的溢出脱力
    如果(XPOS + rootWidth>屏幕宽度)
        XPOS =屏幕宽度 -  rootWidth  -  20;

    // XPOS =((屏幕宽度 -  rootWidth)/ 2)+ xOffset;
    yPos = anchorRect.top  -  rootHeight + yOffset;

    this.rootRectangle =新的矩形(XPOS,yPos,XPOS + rootWidth,yPos + rootHeight);

    布尔onTop =真;
    //显示底部
    如果(rootHeight> anchorRect.top){
        onTop = FALSE;
        yPos = anchorRect.bottom + yOffset;
        this.window.setAnimationStyle(R.anim.grow_from_top);
    }

    this.window.showAtLocation(this.anchor,Gravity.NO_GRAVITY,XPOS,yPos);
 

解决方案

终于想通了什么问题。在code以上我用.measure(),然后.getMeasuredWidth /身高()来获取我想要的位置来看的测量。不幸的是,这些人实际上比的getWidth()和getHeight()已显示的视图后的值以下。我结束了覆盖onSizeChanged使被显示的视图后,我会保持一定的尺寸的轨道。一旦有人显示,我抓住正确的宽度/高度,并立即显示在其正确的现在的位置弹出。

希望这可以帮助别人,虽然我不知道为什么getMeasuredWidth /高分别为返回较小的值。

I'm currently trying to position a popup window dynamically on screen based on an anchor element. For some reason, when I position this element it appears about 100px to the left of where I want it. For instance, if I position it on an anchor near the edge of the screen the popup window should appear with its right edge just to the left of the anchors right edge. However, the popup window appears flush to the edge of the screen. If I subtract 100 from my calculate xPos then it works fine.

I have checked my math and have stepped through the display code to check that the values are not coming out incorrectly, so I'm not sure what is causing the problem. I'm just learning views in android though, so I'm sure I am overlooking something. Hopefully someone here will know!

I've posted the display code below, and would be happy to post anything else if needed.

Thanks in advance!

    int[] location = new int[2];
    this.anchor.getLocationOnScreen(location);

    Rect anchorRect =
            new Rect(location[0], location[1], location[0] + this.anchor.getWidth(), location[1]
                + this.anchor.getHeight());
    this.anchorRectangle = anchorRect;


    this.root.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    this.root.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

    int rootWidth = this.root.getMeasuredWidth();
    int rootHeight = this.root.getMeasuredHeight();

     int screenWidth = this.windowManager.getDefaultDisplay().getWidth();

    int xPos, yPos;
    // Align popup with right side if the root is wider than anchor
    // Align with left otherwise
    if (rootWidth > anchorRect.right - anchorRect.left) {
        xPos = anchorRect.right - rootWidth;
    } else {
        xPos = anchorRect.left + 15;
    }

    // Correct for spilling off the edge
    if (xPos + rootWidth > screenWidth)
        xPos = screenWidth - rootWidth - 20;

    // xPos = ((screenWidth - rootWidth) / 2) + xOffset;
    yPos = anchorRect.top - rootHeight + yOffset;

    this.rootRectangle = new Rect(xPos, yPos, xPos + rootWidth, yPos + rootHeight);

    boolean onTop = true;
    // display on bottom
    if(rootHeight > anchorRect.top) {
        onTop = false;
        yPos = anchorRect.bottom + yOffset;
        this.window.setAnimationStyle(R.anim.grow_from_top);
    }

    this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);

解决方案

Finally figured out what the problem was. In the code above I use .measure() and then .getMeasuredWidth/Height() to get the measurements of the view that I am trying to position. Unfortunately these were actually less than the values of getWidth() and getHeight() after the view had been displayed. I ended up overriding onSizeChanged so that I could keep track of the size after the view was displayed. Once the view was displayed, I grab the correct width/height and display the popup in its correct postion immediately.

Hope this helps someone else, though I'm not sure why getMeasuredWidth/Height were returning smaller values.

这篇关于在Android的定位弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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