如何获得从视图到手机底部的距离? [英] How to get distance from the View to a phone bottom?

查看:67
本文介绍了如何获得从视图到手机底部的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在布局上有某个视图(例如ImageView),是否可以找到从视图底部边框到电话屏幕底部的距离?

If I have a certain View on the layout (ImageView, for example), is it possible to find the distance from the bottom border of the View to the bottom of the phone screen?

谢谢.

推荐答案

// instantiate DisplayMetrics
DisplayMetrics dm = new DisplayMetrics(); 
// fill dm with data from current display        
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
// loc will hold the coordinates of your view
int[] loc = new int[2];
// fill loc with the coordinates of your view (loc[0] = x, looc[1] = y)
yourImageView.getLocationOnScreen(loc);
// calculate the distance from the TOP(its y-coordinate) of your view to the bottom of the screen
int distance = dm.heightPixels - loc[1];

这篇关于如何获得从视图到手机底部的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆