是否可以使用引用其他视图的约束而不将它们放在相对布局中?
换句话说,有没有办法做到这一点:
<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约束。这是可能的,或类似的东西?
不,据我所知,您不能将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
。
由于我错过了一个中心点,Width
和Height
应该绑定到父母的同一属性,让我扩展我的答案。
当然你可以将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)。