【问题标题】:in OnAttachedToWindow(...), GetWindowVisibleDisplayFrame() returns 0 for all dimensions在 OnAttachedToWindow(...) 中,GetWindowVisibleDisplayFrame() 为所有维度返回 0
【发布时间】:2018-03-08 12:59:10
【问题描述】:

我正在尝试在 OnAttachedToWindow 中更改我的活动的大小,以便它看起来是浮动的。为了计算它的新尺寸,我需要获取装饰视图的可见显示框架。但它返回 0。

我实际上需要获取状态栏的高度。我遵循了这个指南:How to get Android screen height minus status bar in pixel?

但它不起作用。

如何获取 OnAttachedToWindow 中状态栏(或可见显示框)的高度?

根据 Android 文档,我认为这是可能的,因为显然它已被附加,并且可以开始绘图了。

这是我的活动代码:

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);
}

【问题讨论】:

    标签: android android-layout xamarin xamarin.android


    【解决方案1】:

    文档实际上是关于OnAttachedToWindow():

    ...它可以在第一个 onDraw 之前的任何时间被调用——包括在 onMeasure(int, int) 之前或之后。

    这就是为什么我不会真正依赖 OnAttachedToWindow()/OnDetachedToWindow() 与 OnMeasure() 或 OnLayout() 不同步。

    是否可以重写 OnSizeChanged() 方法以了解何时测量并在其中应用您想要的内容?

    参考:OnSizeChanged(int w, int h, int oldw, int oldh)

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2021-06-08
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多