Android view's getTop(), getLeft(), getX(), getY(), getWidth(), getHeight() methods

后端 未结 3 1776
我寻月下人不归
我寻月下人不归 2021-02-19 08:02

I am writing a drag and drop application and got really confused because of some parameters.

Please help to figure out.

First of all, I read the documentation fo

3条回答
  •  时光说笑
    2021-02-19 08:33

    This is a supplemental answer for future visitors.

    enter image description here

    • Left, Top: When a parent view lays out a subview, left is the distance from the left side of the parent to the left side of the subview. Likewise, top is the distance from the top of the parent to the top of the subview. Thus, getLeft() and getTop() return the coordinates of the top left corner of the view relative to its parent view (not the absolute coordinates on the screen).
    • X, Y: Usually getX() and getY() will return the same thing as getLeft() and getTop(). However, sometimes it is useful to move the view a little after it has already been laid out. This can be done with setTranslationX() and setTranslationY(). If these have been set then x and y will be different from left and top, where

      x = left + translationX
      y = top + translationY
      
    • Width, Height: You can find the width and the height of the view with getWidth() and getHeight(). This is not affected by a translation.

    The above values are all in pixel dimensions.

提交回复
热议问题