您的位置:首页 > 其它

获得View在布局和屏幕上的位置(getLeft()以及getLocationOnScreen())

2012-11-18 03:09 465 查看
在一个布局中有一个控件View v.

可以通过以下方法可得到其在布局中的位置:

v.getLeft();

v.getRight();

v.getBottom();

v.getTop();

这样就得到控件本身的上下左右四个方位相对于parent的位置.即自身的上下左右距离parent有几个像素

但是请注意:getLeft()表示自己的左边距离parent的左边的距离

但是请注意:getRight()表示自己的右边距离parent的左边的距离

但是请注意:getTop()表示自己的上边距离parent的上边的距离

但是请注意:getBottom()表示自己的下边距离parent的上边的距离

同理:v.layout(l, t, r, b)方法中的参数也是如此

使用如下方法得到,某控件在屏幕上的位置:

int v_location []=new int [2];

v.getLocationOnScreen(v_location);

int step_ball_location_0=v_location[0];

int step_ball_location_1=v_location[1];

即step_ball_location_0和step_ball_location_1即为位置

注意:不可以在OnCreate()方法中执行!!!!此时的step_ball_location_0和step_ball_location_1均为0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐