C# 为什么Xamarin.Form XAML文件中StackLayout的子元素不在屏幕上?

C# 为什么Xamarin.Form XAML文件中StackLayout的子元素不在屏幕上?,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,在我的Xamarin.Form项目中,我有几个按钮,它们是XAML文件中StackLayout(Orientation=“Horizontal”)的子元素。这些按钮的大小不会自动调整,因此所有按钮都可以显示在任何设备的屏幕上。它们太大了,有些会从屏幕上消失 我已经将上面提到的StackLayout包含在另一个StackLayout中,该StackLayout的宽度限制为设备屏幕宽度的100%。我尝试了一些按钮和布局的水平选项,但没有帮助 <ContentPage.Content>

在我的Xamarin.Form项目中,我有几个按钮,它们是XAML文件中StackLayout(Orientation=“Horizontal”)的子元素。这些按钮的大小不会自动调整,因此所有按钮都可以显示在任何设备的屏幕上。它们太大了,有些会从屏幕上消失

我已经将上面提到的StackLayout包含在另一个StackLayout中,该StackLayout的宽度限制为设备屏幕宽度的100%。我尝试了一些按钮和布局的水平选项,但没有帮助

<ContentPage.Content>
    <RelativeLayout>
        <StackLayout
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height,Factor=0.5,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}"
            BackgroundColor="Aqua">
            <Image Source="map.png" />
        </StackLayout>
        <StackLayout
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.5,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
            <Entry Text="pls input here" />
            <StackLayout
                RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}"
                Orientation="Horizontal">
                <Button Text="button1"/>
                <Button Text="button2"/>
                <Button Text="button3"/>
                <Button Text="button4"/>
                <Button Text="button5"/>
            </StackLayout>
        </StackLayout>
    </RelativeLayout>
</ContentPage.Content>

我期望的是,就像我上传的图片中的模型一样,屏幕被一分为二,下半部分(在StackLayout中)将包含一个条目,允许用户输入一些文本,下面5个按钮(在StackLayout中)将水平放置在屏幕内(应自动调整其尺寸,以适应装置的各种尺寸)

也许我甚至不应该在区域的下半部分使用StackLayout?但我确实认为我应该在外部使用RelativeLayout,以便屏幕在任何设备中总是对半分割

非常感谢您的帮助。

使用a可以轻松满足您的要求:

 <StackLayout
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.5,Constant=0}"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
        <Entry Text="pls input here" />
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="50" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button Text="Button1" Grid.Row="0" Grid.Column="0" />
            <Button Text="Button2" Grid.Row="0" Grid.Column="1" />
            <Button Text="Button3" Grid.Row="0" Grid.Column="2" />
            <Button Text="Button4" Grid.Row="0" Grid.Column="3" />
            <Button Text="Button5" Grid.Row="0" Grid.Column="4" />
        </Grid>
    </StackLayout>

A还正在开展工作:

<StackLayout
        RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=0.5,Constant=0}"
        RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width,Factor=1,Constant=0}">
    <Entry Text="pls input here" />

    <FlexLayout Direction="Row"
            AlignItems="Center"
            JustifyContent="SpaceEvenly">

        <Button Text="Button1" />
        <Button Text="Button2" />
        <Button Text="Button3" />
        <Button Text="Button4" />
        <Button Text="Button5" />

    </FlexLayout>

</StackLayout>


基本上,似乎你的按钮是大的。你可以考虑一个包裹布局。不确定它是否在当前的库中可用,但是有一些扩展:按钮的数量是固定的还是动态的?如果你是固定的,你可以把它变成一个网格。按钮的数量是固定的!让我先试试网格布局!谢谢!还有@斯特凡,我。“我在使用Nuget的VS 2019和Xamarin Form 4.1,工具箱中没有看到WarpLayout,可能它已被现有的布局控件所取代?FlexLayout在这种情况下也可以工作。我的解决方案对您有效吗?谢谢大家!最终我使用grid解决了这个问题!”!