表单XAML RelativeLayout:定义一个高度,动态填充其余高度

表单XAML RelativeLayout:定义一个高度,动态填充其余高度,xaml,xamarin.forms,Xaml,Xamarin.forms,使用Xamarin.Forms和XAML,我需要为不同高度的屏幕大小定义一个布局 以下是我得到的结果,以及下面的代码: (为了方便起见,示例代码使用图像和StackLayout所在的框) 下面是发生的事情和我遇到的问题: 蓝色区域是网格,是最底部的元素 黄色区域是相对区域,填充其所在的网格行 橙色区域是必须放置图像的位置,以及图像的大小 必须是,一个正好是布局宽度一半的正方形 绿色区域是StackLayout应该去的地方,除非它应该去 向下延伸以覆盖其下方的所有黄色区域,这是 关键是:不

使用Xamarin.Forms和XAML,我需要为不同高度的屏幕大小定义一个布局

以下是我得到的结果,以及下面的代码:

(为了方便起见,示例代码使用图像和StackLayout所在的框)


下面是发生的事情和我遇到的问题:

  • 蓝色区域是网格,是最底部的元素
  • 黄色区域是相对区域,填充其所在的网格行
  • 橙色区域是必须放置图像的位置,以及图像的大小 必须是,一个正好是布局宽度一半的正方形
  • 绿色区域是StackLayout应该去的地方,除非它应该去 向下延伸以覆盖其下方的所有黄色区域,这是 关键是:不同设备的高度不同, 所以它不能用一个因子来定义。[顺便说一句,我不知道为什么在这个例子中它有任何高度,但它有]
所以。。。如何动态拉伸StackLayout以填充较低的黄色区域,同时确保图像的大小与布局宽度的比率一致?

请尝试以下代码

    <Grid
        BackgroundColor="Blue"
        Padding="10">
        <Grid.RowDefinitions>
            <RowDefinition
                Height="1*" />
            <RowDefinition
                Height="8*" />
            <RowDefinition
                Height="1*" />
        </Grid.RowDefinitions>

        <StackLayout Spacing="0" Padding="0" Margin="0" Grid.Row="1">
            <RelativeLayout Margin="0" Padding="0" VerticalOptions="Start"
                BackgroundColor="Yellow">
                <BoxView
                    x:Name="whereImageGoes"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
                    BackgroundColor="Orange" />
            </RelativeLayout>

            <BoxView
                    x:Name="whereStackLayoutGoesButShouldFillAllYellowAreaUnderIt"
                    VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                    BackgroundColor="Green" />
        </StackLayout>

    </Grid>


太棒了。授予。当Xamarin做对了,它是如此简单!
    <Grid
        BackgroundColor="Blue"
        Padding="10">
        <Grid.RowDefinitions>
            <RowDefinition
                Height="1*" />
            <RowDefinition
                Height="8*" />
            <RowDefinition
                Height="1*" />
        </Grid.RowDefinitions>

        <StackLayout Spacing="0" Padding="0" Margin="0" Grid.Row="1">
            <RelativeLayout Margin="0" Padding="0" VerticalOptions="Start"
                BackgroundColor="Yellow">
                <BoxView
                    x:Name="whereImageGoes"
                    RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
                    BackgroundColor="Orange" />
            </RelativeLayout>

            <BoxView
                    x:Name="whereStackLayoutGoesButShouldFillAllYellowAreaUnderIt"
                    VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                    BackgroundColor="Green" />
        </StackLayout>

    </Grid>