Le Mot Juiced
Le Mot Juiced

Reputation: 3851

Xamarin.Forms XAML RelativeLayout: define one height, have the rest filled dynamically

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:

layout not working

(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

Answers (1)

cahyo
cahyo

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>

enter image description here

Upvotes: 1

Related Questions