[英]in OnAttachedToWindow(…), GetWindowVisibleDisplayFrame() returns 0 for all dimensions
I'm attempting to change the size of my activity in OnAttachedToWindow, so that it appears floating. 我试图在OnAttachedToWindow中更改活动的大小,以使其显示为浮动。 In order to calculate it's new size, I need to get the visible display frame for the decor view. 为了计算新尺寸,我需要获得装饰视图的可见显示框架。 But it returns 0. 但它返回0。
I actually need to get the height of the status bar. 我实际上需要获取状态栏的高度。 I followed this guide: How to get Android screen height minus status bar in pixel? 我遵循了本指南: 如何获取Android屏幕高度减去状态栏(以像素为单位)?
But it doesn't work. 但这是行不通的。
How can I get the height of the status bar (or the visible display frame) in OnAttachedToWindow? 如何在OnAttachedToWindow中获取状态栏(或可见显示框)的高度?
I would have thought this possible since, apparently, it has been attached, and drawing can commence, according to the Android docs. 根据Android文档,我本来以为这是可行的,因为显然已经将其连接起来,并且可以开始绘制了。
Here is my Activity code: 这是我的活动代码:
public override void OnAttachedToWindow()
{
base.OnAttachedToWindow();
var decorView = Window.DecorView;
var rect = new Rect();
decorView.GetWindowVisibleDisplayFrame(rect);
// rect is now 0
var lp = (WindowManagerLayoutParams)decorView.LayoutParameters;
// change some stuff...
WindowManager.UpdateViewLayout(decorView, lp);
}
The docs actually state this about OnAttachedToWindow()
: 文档实际上声明了有关OnAttachedToWindow()
:
...it may be called any time before the first onDraw -- including before or after onMeasure(int, int). ...可以在第一个onDraw之前的任何时间调用-包括在onMeasure(int,int)之前或之后。
That's why I wouldn't really rely on OnAttachedToWindow()/OnDetachedToWindow()
being in sync with neither OnMeasure()
or OnLayout()
. 这就是为什么我不会真正依赖OnAttachedToWindow()/OnDetachedToWindow()
与OnMeasure()
或OnLayout()
都不同步的OnLayout()
。
Is it an idea to override OnSizeChanged()
method to know when it's measured and apply what you want in there? 重写OnSizeChanged()
方法以知道何时对其进行测量并在其中应用所需的想法吗?
Reference: OnSizeChanged(int w, int h, int oldw, int oldh) 参考: OnSizeChanged(int w,int h,int oldw,int oldh)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.