![](/img/trans.png)
[英]Android: Calling setY with the SAME y position on a SeekBar twice moves it into two different locations?
[英]Android set view position - setY vs setTop
我打算以編程方式移動按鈕的位置。 按鈕處於相對布局中。 我研究過並發現我們可以使用.setY()
或.setTop()
。 它們看起來應該是一樣的。
但在我的情況下, .setTop()
根本不會改變位置,而.setY()
只能起作用。 我不確定我做了什么誤會,但對我來說這很奇怪。
有沒有人可以正確解釋setY()
vs setTop()
? 有什么不同?
這是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>
之間的主要區別setY()
和setTop()
是setY()
設置頂部相對於可視區域的視圖的偏移,而setTop()
設置頂部相對於其父視圖的偏移量。
來自Android文檔 。
塞蒂()
設置此視圖的視覺y位置(以像素為單位)。 這相當於將
translationY
屬性設置為傳入的y
值與當前top
屬性之間的差異。
機頂盒()
設置此視圖相對於其父級的頂部位置。
您可以注意到setTop()
與setY()
不同,它沒有效果。 原因是layout()
正在改變字段mTop
。 查看更改的mTop
的最簡單方法是設置上邊距。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.