SetX, setTranslationX, setLeft, LayoutParam.leftMargin, Matrix .. What's the difference

Other questions about setting view position. If you want to move the view around, you can do setX , setTranslationX , setLeft or LayoutParam.leftMargin Offcourse also cancels the onDraw method and uses the Matrix / Raster / Canvas operation.

I was wondering what the difference is. Do they all fully customize the same value, which is the x coordinate for the view? It would be nice to have all this in one post for future readers.

Still know

  • LayoutParam.LeftMargin: available for all APIs (especially for Api 8)
  • SetTranslation: The difference between the original left border of the view and the new left. Although I heard, perhaps this is not persistent?

Can anyone shed light on the differences and do they affect different properties or the same properties?

+7
android
source share
1 answer

I believe that the main difference between setLeft and setX() is that setLeft() refers to its parent view, where setX() just sets the position relative to the whole screen, which is the same as setTranslationX() .

In terms of setLeft() this means that the layout system can reposition the layout. For example, if we used setLeft when scrolling through a RecyclerView, the system would change its position accordingly, so proper use in this case would be setX() .

+2
source share

All Articles