使用Xamarin.form Xaml的AbsoluteLayout.LayoutBounds位置

时间:2016-08-01 11:09:06

标签: xaml layout xamarin xamarin.forms

在Xamarin.form Xaml中:当我给出AbsoulteLayout layoutBounds(.5,.5,.5,.5)时,它会出现在中心但不适用于ios ......如果我正在使用(.75,。 75,.5,.5)它的中心为ios而不是用于Xamarin形式的xaml页面.....如何为ios和android提供相同的layoutBounds

举例:

<AbsoluteLayout BackgroundColor="#99000000"
                    HorizontalOptions="FillAndExpand"                   
                    IsVisible="{Binding xyz}"
                    VerticalOptions="FillAndExpand">

      <AbsoluteLayout x:Name="xyz"
                     AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 0.5"
                     AbsoluteLayout.LayoutFlags="All"
                       BackgroundColor="Transparent"
                       HorizontalOptions="CenterAndExpand"
                       VerticalOptions="CenterAndExpand" />
</AbsoluteLayout>

3 个答案:

答案 0 :(得分:5)

感谢您的回复。 我已经解决了与绝对布局中心对齐有关的问题,希望这是有帮助的

    <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"    IsVisible="{Binding ShowPopup}">
        <BoxView Color="#99000000" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" />
        <StackLayout x:Name="Popup" Padding="6" Orientation="Horizontal" BackgroundColor="Transparent" AbsoluteLayout.LayoutBounds="0.5, 0.5, -1, -1" AbsoluteLayout.LayoutFlags="PositionProportional">
        </StackLayout>
    </AbsoluteLayout>

答案 1 :(得分:3)

您可以使用OnPlatform在不同平台上使用不同的值。

例如:

<ContentPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
      <OnPlatform.iOS>
        0, 20, 0, 0
      </OnPlatform.iOS>
      <OnPlatform.Android>
        0, 0, 0, 0
      </OnPlatform.Android>
      <OnPlatform.WinPhone>
        0, 0, 0, 0
      </OnPlatform.WinPhone>
    </OnPlatform>
  </ContentPage.Padding>

修改代码(未经测试):

<AbsoluteLayout BackgroundColor="#99000000"
                    HorizontalOptions="FillAndExpand"                   
                    IsVisible="{Binding xyz}"
                    VerticalOptions="FillAndExpand">

      <AbsoluteLayout x:Name="xyz"                     
                       AbsoluteLayout.LayoutFlags="All"
                       BackgroundColor="Transparent"
                       HorizontalOptions="CenterAndExpand"
                       VerticalOptions="CenterAndExpand" >
            <AbsoluteLayout.LayoutBounds>
                     <OnPlatform x:TypeArguments="Rectangle">
                        <OnPlatform.iOS>
                              0.75, 0.75, 0.5, 0.5
                        </OnPlatform.iOS>
                        <OnPlatform.Android>
                              0.5, 0.5, 0.5, 0.5
                        </OnPlatform.Android>
                        <OnPlatform.WinPhone>
                              0, 0, 0, 0
                        </OnPlatform.WinPhone>
                     </OnPlatform>
            </AbsoluteLayout.LayoutBounds>
      </AbsoluteLayout>
</AbsoluteLayout>

答案 2 :(得分:0)

这样,我们可以为每个控件在LayoutBounds中的AbsoluteLayout中使用平台指令

<Label AbsoluteLayout.LayoutFlags="PositionProportional">
  <Label.AbsoluteLayout.LayoutBounds>
         <OnPlatform x:TypeArguments="Rectangle" iOS="..." Android="..." /> 
    </Label.AbsoluteLayout.LayoutBounds>
</Label>