Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms - position element on top of another.

I've got a page in my Xamarin.Forms project that I need to look something like this:

enter image description here

I can position the red and blue boxes, no problem. But I can't figure out how to create the green one.

The XAML I use is the following:

<StackLayout HorizontalOptions="FillAndExpand">
    <StackLayout VerticalOptions="FillAndExpand" BackgroundColor="Red">   
         //I'm thinking the green box should be positioned absolutely in here or something?         
    </StackLayout>
    <StackLayout VerticalOptions="End" BackgroundColor="Blue">
        <Grid VerticalOptions="EndAndExpand">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Column="0" Text="Button" HorizontalOptions="FillAndExpand" BorderRadius="0" VerticalOptions="EndAndExpand" HeightRequest="50" TextColor="White" BackgroundColor="#85C034"></Button>
        </Grid>
    </StackLayout>
</StackLayout>
like image 827
Fayze Avatar asked Sep 17 '17 19:09

Fayze


1 Answers

The Grid is the best layout control to implement this behaviour. You can use * to expand a row out to fill, nest the text label inside a content view in the first row and then set the second row to the desired height.

EG:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:Grids"
    x:Class="Grids.GridsPage">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="60"/>
        </Grid.RowDefinitions>
        <ContentView 
            BackgroundColor="Red" Grid.Row="0">
            <Label TextColor="Black"
                Text="Text"
                Margin="0,0,0,10"
                BackgroundColor="#00FF02"
                VerticalOptions="End"
                HorizontalOptions="Center"/>
        </ContentView>
        <ContentView BackgroundColor="Blue" Grid.Row="1"/>
    </Grid>
</ContentPage>

enter image description here

like image 136
matthewrdev Avatar answered Oct 11 '22 02:10

matthewrdev