I ended up solving this issue by determining the height of the status/notification bar like so:
View globalView = ...; // the main view of my activity/application DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); int topOffset = dm.heightPixels - globalView.getMeasuredHeight(); View tempView = ...; // the view you'd like to locate int[] loc = new int[2]; tempView.getLocationOnScreen(loc); final int y = loc[1] - topOffset; 这段代码的作用是,在View中(不包含title)计算某个view相对Main View的y坐标
int[] location = new int[2] ;
view.getLocationInWindow(location); //获取在当前窗口内的绝对坐标
view.getLocationOnScreen(location);//获取在整个屏幕内的绝对坐标
location [0]--->x坐标,location [1]--->y坐标
如果在Activity的OnCreate()事件输出那些参数,是全为0,要等UI控件都加载完了才能获取到这些。
本文介绍了如何在Android应用中通过获取View的测量高度和窗口管理器的显示元数据,来计算某个View相对于主View的y坐标。通过这种方式,开发者可以更精确地定位和调整界面元素,提升用户体验。
1028

被折叠的 条评论
为什么被折叠?



