AbsoluteLayout不填充屏幕

时间:2016-04-18 11:26:42

标签: android xamarin xamarin.forms

我在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仿真器,但我也在设备上获得相同的行为,例如三星galaxy 6)

1px white line on 6.0.0 (you are gona have to zoom)

在这个例子中,我使用boxview对屏幕进行填充,但它的任何位置都与屏幕底部对齐。

我正在寻找的是某种变通方法或自定义渲染器,它将确保以1,1拉伸或拉伸整个屏幕高度的项目放置在屏幕底部或伸展到屏幕底部

1 个答案:

答案 0 :(得分:6)

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