Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

Another questions about setting position of a view. If you want to move a view around then you can do setX, setTranslationX, setLeft or LayoutParam.leftMargin Offcourse there is also overriding the onDraw method and using Matrix/Bitmap/Canvas operation.

I was wondering what's the difference. Do they all ultimetly adjust the same value which is x coordinate of the view? It would be good to have it all in one post for future readers as well

So far I know,

  • LayoutParam.LeftMargin: is available for all API (specific under Api 8)
  • SetTranslation: Difference between original left bound of the view and the new leftbound. Though I heard maybe it is not persistent?

Anyone can shed light on the differences and if they impact different properties or the same property?

like image 877
Snake Avatar asked Mar 06 '15 16:03

Snake


1 Answers

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

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

like image 85
box Avatar answered Nov 09 '22 22:11

box