setX(), setTranslationX(), setY(), and setTranslationY()

后端 未结 3 1733
面向向阳花
面向向阳花 2021-02-05 05:50

What are the differences between setX(), setY(), and setTranslationX(), setTranslationY()? Are they setting offsets on existing coordinates or setting absolute positions?

相关标签:
3条回答
  • 2021-02-05 06:15

    Setting X and Y sets the position of the view relative to the parent view's top left corner (not the absolute position on the screen). Setting the translation moves the view relative to where the parent laid it out. (The parent laid the view out at (left, top).)

    enter image description here

    0 讨论(0)
  • 2021-02-05 06:25

    setX/Y set absolute position from 0,0 screen. setTranlsationX/Y set position from default position of view in layout.

    enter image description here

    0 讨论(0)
  • 2021-02-05 06:30

    From the docs, setTranslationX is:

    Sets the horizontal location of this view relative to its left position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.

    And setX is:

    Sets the visual x position of this view, in pixels. This is equivalent to setting the translationX property to be the difference between the x value passed in and the current left property.

    Thus you can think of setTranlsationX as a relative offset: move 3 pixels left of where you normally would be. And setX is a fixed position: move whatever you have to so that you end up drawing at coordinate X.

    0 讨论(0)
提交回复
热议问题