使用填充居中布局 - 舍入错误

时间:2016-05-11 08:44:50

标签: xamarin xamarin.forms

xamarin中的嵌套布局似乎偏向屏幕左侧。我假设这是dp到像素舍入偏差或什么?

任何人都可以确认,有解决办法吗?

虽然我的示例使用绝对布局,但问题似乎出现在所有布局上。

using System;
using Xamarin.Forms;

namespace XamarinTest
{
    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
            AbsoluteLayout child = layout;
            AbsoluteLayout.SetLayoutFlags(child, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(child, new Rectangle(0, 0, 1, 1));
            Random rand = new Random();
            for (int i =0;i<100; i++)
            {
                child = addLayout(child, rand) ;
            }

            AbsoluteLayout abs = new AbsoluteLayout();
            AbsoluteLayout.SetLayoutFlags(abs, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(abs, new Rectangle(0, 0, 0.5, 0.5));
            abs.BackgroundColor = Color.Black;
            layout.Children.Add(abs);
        }

        private AbsoluteLayout addLayout(AbsoluteLayout parent, Random rand)
        {
            AbsoluteLayout abs = new AbsoluteLayout();
            AbsoluteLayout.SetLayoutFlags(abs, AbsoluteLayoutFlags.All);
            AbsoluteLayout.SetLayoutBounds(abs, new Rectangle(0,0,1,1));
            abs.Padding = new Thickness(2.0);
            abs.BackgroundColor = new Color(rand.NextDouble(), rand.NextDouble(), rand.NextDouble());
            parent.Children.Add(abs);

            return abs;
        }
    }
}

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" x:Name="layout">

  </AbsoluteLayout>
</ContentPage>

屏幕截图(如果上传):

layout centering problem

1 个答案:

答案 0 :(得分:3)

我认为你看到模拟器渲染的差异与Android上运行的Forms代码的问题有关,因为模拟屏幕的DPI可能会使偏移成为混合。

根据实际设备,iOS模拟器在1:1渲染方面做得非常出色。 DPI然后根据您的视图设置对显示进行下采样,您的代码将始终将StackLayout呈现在请求的位置。

如果你看一下使用DPI在两个(基于OS-X的)仿真器上运行的代码,它可以干净利落地&#34;到模拟器物理屏幕上的像素:

enter image description here

enter image description here

与没有的人相比:

enter image description here

如果您拥有GenyMotion模拟器的付费版本,则可以使用一对一的缩放选项,提供与iOS模拟器类似的体验。

故事的道德,使用物理设备对布局进行最终检查。一个好方法是使用Xamarin的测试云并为每个应用程序的表单页面添加屏幕截图

相关问题