Xamarin.Forms:在Grid中使用相对约束但没有相对布局?

问题描述 投票:0回答:1

是否可以使用引用其他视图的约束而不将它们放在相对布局中?

换句话说,有没有办法做到这一点:

<ContentPage .... >
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition 
        RelativeLayout.HeightConstraint="{ConstraintExpression 
        Type=RelativeToParent,
        Property=Height,
        Factor=0.1,
        Constant=0}" />
     </Grid.RowDefinitions>

     <Grid.ColumnDefinitions>
       <ColumnDefinition 
         RelativeLayout.WidthConstraint="{ConstraintExpression 
         Type=RelativeToParent, 
         Property=Heigth, 
         Factor=0.1,
         Constant=0}"/>
     </Grid.ColumnDefinitions>
  </Grid>
</ContentPage>

...请注意,这是一个不在相对布局中的网格,而是使用RelativeLayout约束。这是可能的,或类似的东西?

xamarin.forms constraints relativelayout
1个回答
1
投票

不,据我所知,您不能将RelativeLayout约束应用于网格行或列。

但是有一种非常简单的方法可以达到预期的效果:使用GridLength.Star。具有*高度的所有行(对于列都相同)将具有相同的高度。无论如何,高度为2*的行将是高度为*的行的两倍。

要声明整个网格的高度为1/10的行,只需使用以下行定义

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="9*" />
</Grid.RowDefinitions>

并且等效于列

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="9*" />
</Grid.ColumnDefinitions>

你正在使用9*而不是10*,因为第一行(列)中的*和剩下的9*总结为10*,这是* 1/10

Edit

由于我错过了一个中心点,WidthHeight应该绑定到父母的同一属性,让我扩展我的答案。

当然你可以将Grid本身嵌入RelativeLayout中:

<RelativeLayout>
    <Grid RelativeLayout.WidthConstraint="{ConstraintExpression, Type=RelativeToParent, Property=Width}" 
          RelativeLayout.HeightConstraint="{ConstraintExpression, Type=RelativeToParent, Property=Width}">
        <!-- Elided -->
    </Grid>
</RelativeLayout>

这样,Grids width and height are bound to the same property,这将使Grid二次。在Grid中,您可以使用start系统相对于网格设置列和行。由于Grid的宽度和高度相等,因此*行的高度将等于*列的宽度。这有帮助吗?

请注意:根据您的整体布局,RelativeLayout可能会很慢。 (see here)。

© www.soinside.com 2019 - 2024. All rights reserved.