setX()、setY() 和 setTranslationX()、setTranslationY() 有什么区别?他们是在现有坐标上设置偏移量还是设置绝对位置?
如果他们设置绝对坐标,这不会与父布局的约束冲突吗?
假设我们有类似的东西
|查看 1 |查看 2 |查看3|
在 LinearLayout 中,如果我们执行 view2.setX(0);
或 view2.setTranslationX(0);
会导致 View 2 重叠 View 1 或推送 View 1去旁边?
同样,如果我们有一个将 alignParentBottom 设置为 true 的 RelativeLayout 的子级,但我们在代码中手动将该子级的 y 坐标设置为 0。哪个布局规则胜出?
基本上,我对手动坐标如何影响容器的布局规则以及平移和 X/Y 之间的差异感到困惑。如果这看起来微不足道,我提前道歉,但我是来自 iOS 的 Android 新手。
最佳答案
来自文档,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.
还有 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.
因此,您可以将 setTranlsationX
视为相对偏移量:将您通常所在的位置向左移动 3 个像素。而 setX
是一个固定的位置:移动你必须做的任何事情,这样你最终会在坐标 X 处绘制。
关于java - setX()、setTranslationX()、setY() 和 setTranslationY(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701657/