问题描述
Android 查看似乎有2种方法似乎可以做到功能非常相似.
Android View seems to have 2 methods which appears to do some very similar functionality.
1] setTranslationX
& setTranslationY
2] offsetLeftAndRight
& offsetTopAndBottom
1] setTranslationX
& setTranslationY
2] offsetLeftAndRight
& offsetTopAndBottom
有人可以告诉我这些有何不同吗?可以使用[1]但不能使用[2]的方案是什么?反之亦然?
Can anybody tell me how these are different?What is the scenarion in which [1] can be used but not [2]? and vise-versa?
推荐答案
offsetLeftAndRight()
和 offsetTopAndBottom()
会更改布局传递的结果,而setTranslationX()
和 setTranslationY
在布局结果的顶部添加一个附加偏移量,默认值为0.
offsetLeftAndRight()
and offsetTopAndBottom()
alter the result of the layout pass, while setTranslationX()
and setTranslationY
add an additionnal offset on top of the layout result, the default value being 0.
一个结果是,每次布局传递(例如,由 requestLayout()
触发)时,leftAndRight/TopAndBottom偏移量都会重置,而平移则不会.
One consequence is that the leftAndRight / TopAndBottom offset is reset with every layout pass (e.g. triggered by requestLayout()
) while the translation is not.
我的理解是,如果编写自己的布局, offsetLeftAndRight()
和 offsetTopAndBottom()
最有用,而 setTranslationX()
和 setTranslationY
是调整布局结果的通用方法.
My understanding is that offsetLeftAndRight()
and offsetTopAndBottom()
are mostly useful if you write your own Layout, while setTranslationX()
and setTranslationY
are generic methods to tweak the result of a layout.
这篇关于Android-offsetTopAndBottom,setTranslateY之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!