developer9969
developer9969

Reputation: 5236

how to draw this layout in a frame in xamarin forms

I have been trying to have a layout that resembles this one

enter image description here

I have done as follows but does not look anywhere near.

img2 and label1 should have a margin padding from top.

Should it be done with a grid or a stacklayout?

Any suggestions??

<StackLayout>
<Frame Padding="0" CornerRadius="10" BorderColor="Green" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Image Grid.Row="0" Grid.Column="0" 
               Source="icon.png"
               HeightRequest="30" WidthRequest="30"
               HorizontalOptions="Start" />

        <Image Grid.Row="1" Grid.Column="1" 
               Source="icon.png"
               HeightRequest="30" WidthRequest="30"
               HorizontalOptions="Start" 
               VerticalOptions="Start" />

        <Label Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="3"
               Text ="Label1"
               HorizontalOptions="CenterAndExpand" />

        <Image Grid.Row="0" Grid.Column="4" 
               Source="icon.png"
               HeightRequest="30" WidthRequest="30"
               HorizontalOptions="End"></Image>


        <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
               Text ="Label2"
               Margin="20,10,0,0"
               HorizontalOptions="CenterAndExpand" />


        <Label Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
               Text ="Label3"
               Margin="20,10,0,0"
               HorizontalOptions="CenterAndExpand" />

        <Label Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="4"
               Text ="Label4 and this is some long text"
               Margin="20,10,0,0"
               HorizontalOptions="CenterAndExpand" />
    </Grid>
</Frame>

Upvotes: 0

Views: 1966

Answers (1)

Geoffrey Fielden
Geoffrey Fielden

Reputation: 26

A grid is fine for the type of layout, whenever I have complex layouts to do like this I tend to just make the first row into a grid itself to have better control over my layout.

This should give you what you need as far as the alignment goes.

<StackLayout>
    <Frame Padding="0"
           CornerRadius="10"
           BorderColor="Green">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid Grid.Row="0"
                  Grid.ColumnSpan="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0"
                       Source="icon.png"
                       VerticalOptions="StartAndExpand"
                       HorizontalOptions="Start"
                       HeightRequest="30"
                       WidthRequest="30" />

                <Image Grid.Column="1"
                       Source="icon.png"
                       Margin="0,10,0,0"
                       HeightRequest="30"
                       HorizontalOptions="CenterAndExpand"/>

                <Label Grid.Column="2"
                       Text="Label1"
                       Margin="0,10,0,0"
                       HeightRequest="30"
                       HorizontalOptions="CenterAndExpand" />

                <Label Grid.Column="4"
                       Source="icon.png"
                       HeightRequest="30"
                       WidthRequest="30"
                       VerticalOptions="StartAndExpand"
                       HorizontalOptions="End" />

            </Grid>


            <Label Grid.Row="1"
                   Grid.ColumnSpan="1"
                   Text="Label2"
                   Margin="20,10,20,0"
                   HorizontalOptions="FillAndExpand" />


            <Label Grid.Row="2"
                   Text="Label3"
                   Grid.ColumnSpan="1"
                   Margin="20,0,20,0"
                   HorizontalOptions="FillAndExpand" />

            <Label Grid.Row="3"
                   Grid.ColumnSpan="2"
                   Text="Label4 and this is some long text"
                   Margin="20,10,20,10"
                   HorizontalOptions="FillAndExpand" />
        </Grid>
    </Frame>
</StackLayout>

For the images on the top row, I set the column width to auto for both ends to make sure that it only takes the space it needs, then I had the center columns fill the rest. All is left is to add the necessary margins and padding.

The 2 columns I left on the main grid are essentially to make sure the two middle rows only take half the space as intented.

Upvotes: 1

Related Questions