Android View的坐标和屏幕高度 getY getX getLett getTop

2021-01-14 06:12:19

参考地址 Android获取View 的坐标和屏幕高度、状态栏高度、标题栏高度


注意事项:

(1)View.getX()和View.getY()获取到的值为相对于父视图而言的两个左边缘和上边缘的距离。

(2)View.getX()和View.getY()值在Activity的onCreate()方法中获取的值很可能为0,因为

此时View很可能没有绘制完成。也不要在此类函数中执行getWidth()和getHeight()方法;


1.获取相对于父ViewGroup的坐标:
getLeft , getTop, getBottom, getRight
2.获取相对于窗口的坐标:
getLocationInWindow


int[] position = new int[2];textview.getLocationInWindow(position);System.out.println("getLocationInWindow:" + position[0] + "," + position[1]);

3.获取相对于屏幕的坐标:


getLocationOnScreen

int[] position = new int[2];textview.getLocationOnScreen(position);System.out.println("getLocationOnScreen:" + position[0] + "," + position[1]);


理解图:


4.获取:屏幕高度、状态栏高度、标题栏高度




屏幕区域的获取:

	activity.getWindowManager().getDefaultDisplay();

         应用区域的获取:


Rect outRect = new Rect();activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);

        view绘制区域获取:


Rect outRect = new Rect();activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);


用绘制区域的outRect.top - 应用区域的outRect.top 即是标题栏的高度。

注意: 如果刚启动Activity时就要计算这些数据,最好在 onWindowFocusChanged  函数中进行, 否则得到的某些数据可能是错误的,比如,应用区域高宽的获取。


  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).

  • 2017-02-16 08:09:01

    HTML中PRE和p的区别

    pre 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。 <pre> 标签的一个常见应用就是用来表示计算机的源代码。

  • 2017-02-16 15:14:14

    动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法。