Android View getX getLeft getTranslationX layoutparams.leftMargin的值

来源:互联网 发布:淘宝客服每日工作内容 编辑:程序博客网 时间:2024/05/17 15:35

转载请标明出处【http://blog.csdn.net/hlglinglong/article/details/42536865

1、概述

Android系统中德坐标系统比较复杂,各种绝对和相对坐标用起来真的很容易混淆。
开发中经常用到View的一些坐标,一些值经常搞混,今天就来总结下。

2、Android View layout()

android.view.View.layout(int l, int t, int r, int b)    
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
文档上写的很清楚,这四个参数都是相对于父视图来说的。
layout的过程就是确定View在屏幕上显示的具体位置,在代码中就是设置其成员变量mLeft,mTop,mRight,mBottom的值,这几个值构成的矩形区域就是该View显示的位置,不过这里的具体位置都是相对与父视图的位置mLeft代表当前view.layout的这个view的左边缘离它的父视图左边缘的距离mTop指当前view的上边缘离父视图上边缘的距离。而以此为界,mRight所指的是当前view的右边缘离父视图左边缘的距离(mLeft+自己的宽度),mBottom也是指当前view的下边缘离父视图的上边缘的距离。至于为何如此,大概是因为坐标系的缘故,坐标中的任何点都必须以(0,0)为起点,XY轴为衡量。

视图左侧位置  view.getLeft() 
视图右侧位置 view.getRight()

视图顶部位置 view.getTop();
视图底部位置 view.getBottom();
这四个方法所获取到的各个左上右下的值与layout的四个参数代表的是一样的,都是相对父视图的左边缘与上边缘。

3.View getTranslationX getTranslationY

he horizontal location of this view relative to its left position. This position is post-layout, in addition to wherever the object's layout placed it.
相对于自己左边(上边)的距离,移动后的数值。

4.View getX() getY()

android.view.View.getX() 
The visual x position of this view, in pixels. This is equivalent to the translationX property plus the current left property.
android.view.View.getY()
The visual y position of this view, in pixels. This is equivalent to the translationY property plus the current top property.
意思就是说获取到实际的View的x y 值,该值也是相对父视图而言的
并且该值 getX = getLeft + getTraslationX。最后得出的值也是相对父视图来说的。

5.LayoutParams getMargins

The left(top right bottom) margin in pixels of the child. 

获取到该View的四个边的边距,有可能是附近的其他子view,也有可能是相对父视图。要看实际的情况分析

一般来说,我们要动态的移动子view的位置,可以使用setmargins 相对父视图来实现,前提是不能跟其他子view发生关系

6.getPadding

Returns the left(top right bottom) padding of this view.
获得该view的内部缩进值(自己内部),相对于Margin(相对于其他view或ViewGroup). 


综上,要获取到view准确的位置,要根据设定去拿值
比如,给view设置margin后,然后用translation移动,最后值就可以用getmargin + getTranslation来计算。etc

1 0
原创粉丝点击