如何从视图到手机底部的距离?

Mar*_*hov 4 android android-layout android-xml

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

谢谢.

Oma*_*dan 7

// 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];
Run Code Online (Sandbox Code Playgroud)


N J*_*N J 1

您可以通过以下方式找到它。

使用它来获取屏幕上的视图位置

View.getLocationOnScreen()和/或getLocationInWindow().

获取屏幕显示

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Run Code Online (Sandbox Code Playgroud)

然后计算(height-view.y-location)