Android setX() and setY() behaving weird

北慕城南 提交于 2019-12-01 16:04:52
hyperum

Really, this shouldn't be happening. Alternatively, try setting another variable and setting x and y to it, or get x and get y and add a 0 to each one of them for same location.

As stated in Android - Use of view.setX() and setY in api 8, if you have searched, there is another solution that also works even before api 8. LayoutParams works like this -

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT
params.leftMargin = 206; //XCOORD
params.topMargin = 206; //YCOORD
childView.setLayoutParams(params);

There is more information there. I hope this helps

Run into the same issue. View.setLeft(int)/View.setTop(int) worked for me.

Note that since the original post of this answer things changed and on the more recent android versions it may produce unexpected results while it did the trick for me on older versions. So if you are targeting older devices (android 3.0 and below) this may help but for a more generic solution please consider other answers here as well.

Is your activity in full screen mode? If no try to make it to full screen and it should solve your problem.

Pretty late to answer, but if someone else is facing the same problem. This fixed it for me it was the paddings in the layout file:

android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"

As someone who actually facing this problem, I have solve this issue by removing any padding in the parentView. The padding seem cause a change in the layout size

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!