Grid with Spacing and BoxView

1

I am learning to use Grid and I have three doubts in my project that even in the tests or the accompanying tutorials I did not get good results in practice since I always saw tests being done with BoxView:

  • What is the best way to give space between components? Because I used HorizontalOption basically I would only have 3 controls ( Start , Center and End ), and this is bad for me since I usually use more; but if using Margin would not the spacing be different depending on the amount of megapixel in the screen of the tested device, so it could be good in my device but in another deformed?
  • I want to use a BoxView in the Grid to separate the lines from ListView and get a better view, but I can not and will leave the code to help me if possible.
  • When viewing the code it will be clear that I use a Label Data: that does not even appear in ListView , I would like to know the reason.
  • Picture of how is my project:

    XAML:

    <StackLayout><ListViewx:Name="lstCompra"  
                  BackgroundColor="LightGray"
                  HasUnevenRows="True">
            <!--  HasUnevenRows = Serve para fazer com que o conteúdo digitado não seja cortado -->
            <ListView.ItemTemplate>
                <!-- DataTemplate  = exibe dados de uma coleção de objetos em um ListView -->
                <DataTemplate>
                    <ViewCell>
                        <Grid>
    
                            <!-- Colunas -->
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="150"/>
                                <ColumnDefinition Width="180"/>
                            </Grid.ColumnDefinitions>
    
                            <!-- Linhas -->
                            <Grid.RowDefinitions>
                                <RowDefinition Height="15"/>
                                <RowDefinition Height="3"/>
                            </Grid.RowDefinitions>
    
                            <!-- Código -->
                            <Label Grid.Row="0" 
                                   Grid.Column="0"
                                   Text="Código:"
                                   FontSize="Small"
                                   BackgroundColor="Yellow"
                                   Margin="2, 0, 0, 0"/>
                            <Label Grid.Row="0" 
                                   Grid.Column="0"
                                   Text="{Binding ID_SOLCOMPRA}" 
                                   FontSize="Small"
                                   BackgroundColor="Green"
                                   TextColor="Black"
                                   Margin="50, 0, 0, 0"/>
    
                            <!-- Data -->
                            <Label Grid.Row="0" 
                                   Grid.Column="1"
                                   Text="Data:"
                                   FontSize="Small"
                                   BackgroundColor="LightCyan"
                                   HorizontalOptions="EndAndExpand"/>
                            <Label Grid.Row="0" 
                                   Grid.Column="1"
                                   Text="{Binding DT_CADASTRO}" 
                                   FontSize="Small"
                                   BackgroundColor="Fuchsia"
                                   TextColor="Black"
                                   HorizontalOptions="End"/>
    
                            <!-- Repartir conteudo com cor -->
                            <BoxView Grid.Row="1" 
                                     Grid.Column="0"
                                     Grid.ColumnSpan="1"
                                     BackgroundColor="Red"/>
    
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    
        
    asked by anonymous 07.06.2018 / 20:54

    1 answer

    2

    The Xamarin documentation provides a general explanation of this layout , so let's get right to your questions:

      
  • What is the best way to give space between components? Because if I use HorizontalOption basically I would only have 3 controls (Start, Center and End), and this is bad for me since I usually use more; but if using Margin would not be different spacing depending on the amount of megapixel on the screen of the tested device, so it could look good on my device but in another deformed?
  •   

    Just making an addition: I am against speeches that attempt to impress opinion on what is or is not 'better'. I can suggest some forms, but the adjective I would assign to them would be ' possible ' (not necessarily better). There may be others. They may or may not be better, I do not intend to enter into this merit. I think each situation will determine what would be 'best' for you in that case.

    HorizontalOptions does not determine a fixed location. It determines a positioning behavior of an element inside a container.

    However, other factors help determine the final positioning of the element. For example, in each case, each cell of Grid acts as a container, but this layout allows overlap , and this causes HorizontalOptions="Start" to more than one element (assuming same size) do with one lay over the other. Hence the need to use artifacts as Margin for this case.

    Alternatively, you can use a StackLayout with Orientation="Horizontal" inside the cell, which will cause the elements to position ( StackLayout can not overlap). >

    In general, when I use Grid I always choose to set a grid (number of rows and columns) where each individual element occupies a single cell and others that need to overlap at certain times are defined to occupy more than one row / column.

      
  • I want to use a BoxView in the Grid to separate the rows from the ListView and get a better view, but I can not and will leave the code to help me if possible.
  •   

    According to your definition of BoxView ( Grid.Row="1" , Grid.Column="0" and Grid.ColumnSpan="1" ), it will be in the first column, second row, and occupy only 1 column width (this is the default behavior) .

    I think your intention here was for it to occupy two columns ( Grid.ColumnSpan="2" ).

      
  • When viewing the code it will be clear that I use a Label Data: which does not even appear in the ListView, I would like to know the reason.
  •   

    In that other answer I mentioned that Grid allows overlapping content. This is exactly the reason, the label that has the text "Date:" was superimposed by the other one that is set in the same position.

    With all of this answered, a possible solution to define your layout would be this:

    <ViewCell>
        <Grid>
    
            <!-- Colunas -->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="180"/>
            </Grid.ColumnDefinitions>
    
            <!-- Linhas -->
            <Grid.RowDefinitions>
                <RowDefinition Height="15"/>
                <RowDefinition Height="3"/>
            </Grid.RowDefinitions>
    
            <!-- Código -->
            <StackLayout Grid.Row="0" 
                         Grid.Column="0"
                         Padding="2"
                         Orientation="Horizontal">
                <Label Text="Código:"
                       FontSize="Small"
                       BackgroundColor="Yellow"/>
                <Label Text="{Binding ID_SOLCOMPRA}" 
                       FontSize="Small"
                       BackgroundColor="Green"
                       TextColor="Black"/>
            </StackLayout>
    
            <!-- Data -->
            <StackLayout Grid.Row="0" 
                         Grid.Column="1"
                         Padding="2"
                         Orientation="Horizontal">
                <Label Text="Data:"
                       FontSize="Small"
                       BackgroundColor="LightCyan"/>
                <Label Text="{Binding DT_CADASTRO}" 
                       FontSize="Small"
                       BackgroundColor="Fuchsia"
                       TextColor="Black"
                       HorizontalOptions="EndAndExpand"
                       HorizontalTextAlignment="End"/>
            </StackLayout>
    
            <!-- Repartir conteudo com cor -->
            <BoxView Grid.Row="1" 
                     Grid.Column="0"
                     Grid.ColumnSpan="2"
                     BackgroundColor="Red"/>
    
        </Grid>
    </ViewCell>
    

    I hope this helps.

        
    10.06.2018 / 18:21