【问题标题】:AbsoluteLayout Doesn't Fill ScreenAbsoluteLayout 不填满屏幕
【发布时间】:2016-04-18 11:26:42
【问题描述】:

我在 android 6.0 Marshmellow 上的 Xamarin Forms 中的绝对布局存在问题

当设置一个视图填充整个屏幕时,屏幕底部会留下 1px 的间隙。

奇怪的是,如果您将屏幕旋转为横向,它就不会发生。或在 4.4

xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinTest.Page1">
  <AbsoluteLayout BackgroundColor="White">
    <BoxView BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">

    </BoxView>
  </AbsoluteLayout>
</ContentPage>

屏幕截图:(这些来自 vs 模拟器,但我在设备上也有相同的行为,例如三星银河 6)

在此示例中,我使用 boxview 填充屏幕,但它的任何位置都与屏幕底部对齐。

我正在寻找的是某种解决方法或自定义渲染器,它将确保在 1,1 处绘制的项目或拉伸屏幕的整个高度、放置或拉伸到屏幕底部

【问题讨论】:

  • 你在 Xamarin.Android 应用程序上得到类似的结果吗?
  • 我还没有制作其中之一。我真的需要一个表单解决方案,因为我们有大量使用绝对布局的页面

标签: android xamarin xamarin.forms


【解决方案1】:

这是 Xamarin.Forms 2.1.0 中的一个已确认错误:https://bugzilla.xamarin.com/show_bug.cgi?id=40092

您可以尝试通过覆盖 LayoutChildren 并向基本实现多发送一个高度像素来修复它。

public class MyAbsoluteLayout : AbsoluteLayout
{
    protected override void LayoutChildren(double x, double y, double width, double height)
    {
        base.LayoutChildren(x, y, width, height+1);
    }
}

然后在你的 XAML 中使用它

<local:MyAbsoluteLayout BackgroundColor="White">
  <BoxView BackgroundColor="Red" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">

  </BoxView>
</local:MyAbsoluteLayout>

如果这没有帮助,您可以尝试重新实现 LayoutChildren 函数并像这样操作结果

public class MyAbsoluteLayout : AbsoluteLayout
{
    private readonly MethodInfo _computeLayout;

    public MyAbsoluteLayout()
    {
        _computeLayout = typeof(AbsoluteLayout).GetTypeInfo().GetDeclaredMethod("ComputeLayoutForRegion");
    }

    protected override void LayoutChildren(double x, double y, double width, double height)
    {
        foreach (View logicalChild in Children)
        {
            Size region = new Size(width, height);
            Rectangle layoutForRegion = (Rectangle)_computeLayout.Invoke(null, new object[] { logicalChild, region });
            layoutForRegion.X += x;
            layoutForRegion.Y += y + 1;
            Rectangle bounds = layoutForRegion;
            logicalChild.Layout(bounds);
        }
    }
}

【讨论】:

  • 谢谢,天哪,我不应该再对这些错误感到惊讶了
  • 仍然遇到这个问题,感谢您发布到错误链接!
猜你喜欢
  • 2020-07-20
  • 2020-01-19
  • 2012-11-02
  • 2019-01-28
  • 2012-04-02
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多