XamarinForms:RelativeLayout 中的 StackLayout 未填满整个屏幕

19

我有这个:

<RelativeLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Red">
                <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="0,15" Spacing="10"></StackLayout>
</RelativeLayout>

但出于某些原因,尽管RelativeLayout明显扩展了,但StackLayout并没有。我该如何使StackLayout水平拉伸并填充整个屏幕的宽度/高度?

1个回答

41

对于RelativeLayout,您需要使用约束来代替垂直/水平选项。应该像这样:

<StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="0,15" Spacing="10"
     RelativeLayout.WidthConstraint=
         "{ConstraintExpression Type=RelativeToParent,
                                Property=Width,
                                Factor=1}"
     RelativeLayout.HeightConstraint=
         "{ConstraintExpression Type=RelativeToParent,
                                Property=Height,
                                Factor=1}">
</StackLayout>

4
我遇到了相同的问题,但这个答案似乎在安卓系统上不起作用。其他控件,比如网格也普遍存在这个问题。 - Ewan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接