setX(),setTranslationX(),setY()和setTranslationY() [英] setX(), setTranslationX(), setY(), and setTranslationY()

查看:326
本文介绍了setX(),setTranslationX(),setY()和setTranslationY()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果它们设置的是绝对坐标,那不会与父布局的约束发生冲突吗?

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

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

在LinearLayout中,如果我们执行 view2.setX(0); view2.setTranslationX(0); ,该怎么办会看到2个重叠视图1或推送视图1去旁边?

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?

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

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?

从本质上讲,我对手动坐标如何影响容器的布局规则以及转换和X/Y之间的差异感到困惑.如果这看起来很微不足道,我会先道歉,但是我是iOS方面的新手.

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.

推荐答案

从文档中

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.

setX 是:

设置此视图的可视x位置(以像素为单位).这等效于将translationX属性设置为传入的x值与当前left属性之间的差.

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处进行绘制.

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.

这篇关于setX(),setTranslationX(),setY()和setTranslationY()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆