android 获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度

摘要:
1.屏幕大小,源代码如下:DisplayMetricsmetrics=newDisplayMetrics();getWindowManager().getDefaultDisplay()。getMetrics(度量);intscreenWidth=度量。宽度像素;//屏幕宽度int.screenHeight=度量。高度像素//屏幕高度

1.屏幕尺寸,源代码如下:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth=metrics.widthPixels;            //屏幕宽度
int .screenHeight=metrics.heightPixels;        //屏幕高度

这段代码可以插入到 Activity 的 onCreate() 函数中。

2.获取标题栏、状态栏高度:

Rect rect = new Rect(); 
Window win = this.getWindow(); 
win.getDecorView().getWindowVisibleDisplayFrame(rect); 
int statusBarHeight = rect.top; 
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 
int titleBarHeight = contentViewTop - Variable.statusBarHeight; //statusBarHeight为状态栏高度,titleBarHeight为标题栏高度

  

getLocationOnScreen 计算该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是包括了通知栏的高度)//获取在当前屏幕内的绝对坐标 

getLocationInWindow 计算该视图在它所在的widnow的坐标x,y值,//获取在整个窗口内的绝对坐标

getLeft , getTop, getBottom, getRight, 这一组是获取相对在它父亲里的坐标


int[] location = new  int[2] ;
view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
view.getLocationOnScreen(location); //获取在整个屏幕内的绝对坐标
location [0]--->x坐标

location [1]--->y坐标

免责声明:文章转载自《android 获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇00、网路调试助手的使用AFO(Away From OI) —— 记我的 OI 生涯下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

metrics-server 安装和报错 Kubernetes metrics-server: kubectl top nodes Error from server (ServiceUnavailable)

安装过程参考https://blog.csdn.net/qq_40460909/article/details/93321945 git clone https://github.com/kubernetes-incubator/metrics-server.git cd metrics-server/deploy/1.8+/ sed -i 's#k8s....

在PyQt中构建 Python 菜单栏、菜单和工具栏

摘要:菜单、工具栏和状态栏是大多数GUI 应用程序的常见且重要的图形组件。您可以使用它们为您的用户提供一种快速访问应用程序选项和功能的方法。 本文分享自华为云社区《Python 和 PyQt:创建菜单、工具栏和状态栏》,作者:Yuchuan。 在使用 Python 和PyQt开发图形用户界面 (GUI)应用程序时,您将使用的一些最有用和最通用的图形元素是...

win32-使用FillRect绘制具有渐变颜色的客户区域背景

void OnEraseBkGnd(HWND hwnd) { /* Vars */ HDC dc; /* Standard Device Context; used to do the painting */ /* rect = Client Rect of the window; Temp = Temparory rec...

常规服务器配置:Prometheus+Grafana监控

准备两台测试环境: 主:192.168.28.130 备:192.168.28.131 博文大纲:一、prometheus简介二、Prometheus组成及架构三、部署prometheus1)环境准备2)部署prometheus3)配置Peometheus监控实现报警 一、prometheus简介 Prometheus是一套开源的系统监控报警框架。它以给定...

[C++ STL] vector使用详解

一、概述 vector(向量): 是一种序列式容器,事实上和数组差不多,但它比数组更优越。一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界。而vector正好弥补了这个缺陷,它的特征是相当于可分配拓展的数组(动态数组),它的随机访问快,在中间插入和删除慢,但在末端插入和删除快。 二、定义及初始化 使用之前必须加相应容器的头文件: #...

iOS UI-文本视图(UITextView)

1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextViewDelegate> 4 5 @property (strong, nonatomic) UITextView *textView; 6 7 @end 8 9 @implement...