Search code examples
xamarinxamarin.formsgrid-layout

Xamarin.Forms Entry.Height is bigger than Label.Height


I have a Xamarin.Forms grid that I'm setting up and I've noticed that the Entry fields are a lot higher (3 - 4 times) than label fields. I can't see any obvious reason why this would be the case.

<Grid RowSpacing="0" ColumnSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="0" Text="G/C" FontSize="Small"/>
    <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red"/>
</Grid>

This results in something like this (on the default Android Emulator, installed 2 days ago); Entry Example

Does anyone have any suggestions as to why the Entry height is so much bigger and what I can do to reduce it (obviously HeightRequest, but I'd rather avoid if possible)?


Solution

  • Based on your comments here is a suggestion to do what you want in XAML. You can bind Entry.HeightRequest to Label.Height to avoid setting it manually.

    <Grid RowSpacing="0" ColumnSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
    
        <Label Grid.Row="0" Grid.Column="0" Text="G/C" FontSize="Small" x:Name="MyLabel" />
        <Entry Grid.Row="1" Grid.Column="0" FontSize="Small" BackgroundColor="Red" HeightRequest="{Binding Source={x:Reference MyLabel}, Path=Height}"/>
    </Grid>