Reputation: 3851
Using Xamarin.Forms and XAML, I need to define a layout for screen sizes of various heights.
Here's the results I'm getting, and the code below it:
(Just for convenience the example code uses Boxes where an Image and StackLayout should go)
<Grid
BackgroundColor="Blue"
Padding="10">
<Grid.RowDefinitions>
<RowDefinition
Height="1*" />
<RowDefinition
Height="8*" />
<RowDefinition
Height="1*" />
</Grid.RowDefinitions>
<RelativeLayout
Grid.Row="1"
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" />
<BoxView
x:Name="whereStackLayoutGoesButShouldFillAllYellowAreaUnderIt"
VerticalOptions="FillAndExpand"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.5}"
BackgroundColor="Green" />
</RelativeLayout>
</Grid>
Here's what's going on, and the problem I'm having:
So... how do I dynamically stretch the StackLayout to fill that lower yellow area while still making sure the image has a consistent ratio of size to the layout's width?
Upvotes: 0
Views: 724
Reputation: 607
Try this code
<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>
Upvotes: 1