Java setX()、setTranslationX()、setY() 和 setTranslationY()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33701657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:55:28  来源:igfitidea点击:

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

javaandroidandroid-layout

提问by Jimmy Wang

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

setX()、setY() 和 setTranslationX()、setTranslationY() 之间有什么区别?他们是在现有坐标上设置偏移量还是设置绝对位置?

If they are setting absolute coordinates won't that conflict with the parent layout's constraints?

如果他们设置绝对坐标会不会与父布局的约束冲突?

Say we have something like | View 1 | View 2 | View 3|

说我们有类似的东西 | View 1 | View 2 | View 3|

in a LinearLayout, what if we did view2.setX(0);or view2.setTranslationX(0);would view 2 overlap view 1 or push view 1 to the side?

在 LinearLayout 中,如果我们查看view2.setX(0);view2.setTranslationX(0);将查看 2 个重叠视图 1 或将视图 1 推到一边怎么办?

Likewise what if we had a child of a RelativeLayout that had alignParentBottom set to true but we manually set that child's y coordinate to 0 in code. Which layout rule wins?

同样,如果我们有一个将 alignParentBottom 设置为 true 的 RelativeLayout 的孩子,但我们在代码中手动将该孩子的 y 坐标设置为 0 会怎样。哪个布局规则获胜?

Essentially, I'm confused about how manual coordinates affect the layout rules of the container and also the difference between translation and X/Y. I apologize in advance if this seems trivial but I am new to Android having come from iOS.

本质上,我对手动坐标如何影响容器的布局规则以及平移和 X/Y 之间的差异感到困惑。如果这看起来微不足道,我提前道歉,但我是来自 iOS 的 Android 新手。

采纳答案by Buddy

From the docs, setTranslationXis:

从文档中,setTranslationX是:

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 setXis:

并且setX是:

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.

设置此视图的视觉 x 位置,以像素为单位。这相当于将 translationX 属性设置为传入的 x 值与当前 left 属性之间的差值。

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

因此,您可以将其setTranlsationX视为相对偏移:将您通常所在的位置向左移动 3 个像素。并且setX是一个固定位置:移动你必须做的任何事情,以便最终在坐标 X 处绘制。

回答by Style-7

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

setX/Y从 0,0 屏幕设置绝对位置。 setTranlsationX/Y从布局中的默认视图位置设置位置。

enter image description here

在此处输入图片说明

回答by Suragch

Setting Xand Ysets 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).)

设置XY设置视图相对于父视图左上角的位置(不是屏幕上的绝对位置)。设置平移会相对于父级布局位置移动视图。(父视图在(left, top).)

enter image description here

在此处输入图片说明