Get positions of views in a dialog, relative to their parent

后端 未结 1 1214
名媛妹妹
名媛妹妹 2020-12-07 03:19

What I want to do is, to draw a line from the edge of a button to a point on the screen...

I\'m using a dialog fragment for this... And all functions I tried always

相关标签:
1条回答
  • 2020-12-07 03:42

    You need to wait for the callback when the layout has placed the children views. The code you are using returns 0 because the position is returned before the layout is placed. Use this code:

    YourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        getViewTreeObserver().removeGlobalOnLayoutListener(this);
    
        int[] locations = new int[2];
        YourView.getLocationOnScreen(locations);
        int x = locations[0];
        int y = locations[1];
    }
    });
    
    0 讨论(0)
提交回复
热议问题