[英]Android set view position - setY vs setTop
I was going to move the button's position programmatically. 我打算以编程方式移动按钮的位置。 Button is in relative layout. 按钮处于相对布局中。 I'd researched and found that we can use .setY()
or .setTop()
. 我研究过并发现我们可以使用.setY()
或.setTop()
。 It looks like they should work the same. 它们看起来应该是一样的。
But in my case, .setTop()
does not change the position at all and .setY()
works only. 但在我的情况下, .setTop()
根本不会改变位置,而.setY()
只能起作用。 I'm not sure what I did misunderstand but it's very weird for me. 我不确定我做了什么误会,但对我来说这很奇怪。
Is there anybody who can explain setY()
vs setTop()
correctly? 有没有人可以正确解释setY()
vs setTop()
? What is the difference? 有什么不同?
This is layout.xml
: 这是layout.xml
:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
The main difference between setY()
and setTop()
is that setY()
sets the top offset of the view relative to the visual area , whereas setTop()
sets the top offset of the view relative to its parent . 之间的主要区别setY()
和setTop()
是setY()
设置顶部相对于可视区域的视图的偏移,而setTop()
设置顶部相对于其父视图的偏移量。
From the Android documentation . 来自Android文档 。
setY() 塞蒂()
Sets the visual y position of this view, in pixels. 设置此视图的视觉y位置(以像素为单位)。 This is equivalent to setting the
translationY
property to be the difference between they
value passed in and the currenttop
property. 这相当于将translationY
属性设置为传入的y
值与当前top
属性之间的差异。
setTop() 机顶盒()
Sets the top position of this view relative to its parent. 设置此视图相对于其父级的顶部位置。
You can notice that setTop()
doesn't have effect unlike setY()
. 您可以注意到setTop()
与setY()
不同,它没有效果。 The reason is that a field mTop
is being changed by layout()
. 原因是layout()
正在改变字段mTop
。 The simplest way to see a changed mTop
is to set a top margin. 查看更改的mTop
的最简单方法是设置上边距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.