Xamarin.Forms:使图像宽度成比例

时间:2016-08-30 09:17:17

标签: image xaml layout xamarin.forms

我想使用一个图像,它的宽度应该是父宽度的1/3。图像的高度应按正确宽高比的宽度缩放。因此,我想控制图像顶部/底部的填充。

当然我可以使用WidthRequest,但那是some kind of fixed values

<Image x:Name="logo"
       Source="logo.png"
       Aspect="AspectFit"
       HorizontalOptions="Center">
  <Image.WidthRequest>
    <OnIdiom x:TypeArguments="x:Double" Phone="150" Tablet="200" />
  </Image.WidthRequest>
</Image>

我可以使用AbsoluteLayoutRelativeLayout,但我不知道应该使用哪个高度。那是因为我想设置一个宽度,高度应该正确地缩放它。例如:

<AbsoluteLayout>
  <Image x:Name="logo"
       Source="Assets/logo.png"
       Aspect="AspectFit"
       AbsoluteLayout.LayoutBounds="0.5,0.5,0.5,1"
       AbsoluteLayout.LayoutFlags="All" />
</AbsoluteLayout>

这样可行,但我无法准确指定底部填充。在一个平台上它是好的,在另一个平台上它没有。现在我尝试了Grid

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="40" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Image x:Name="logo"
       HorizontalOptions="CenterAndExpand"
       Aspect="AspectFit"
       Grid.Row="0"
       Grid.Column="1">
    <Image.Source>
      <OnPlatform x:TypeArguments="ImageSource">
        <OnPlatform.iOS>logo.png</OnPlatform.iOS>
        <OnPlatform.Android>logo.png</OnPlatform.Android>
        <OnPlatform.WinPhone>Assets/logo.png.</OnPlatform.WinPhone>
      </OnPlatform>
    </Image.Source>
  </Image>
</Grid>

在这里,我也无法定义顶级&amp;底部填充。在肖像中,没有,在景观中没有。

有什么建议吗?

0 个答案:

没有答案
相关问题