Topic : Layout Attribute - Sizing: Alignment | Expansion

Sizing:

In Layout, the size of a View depends on Both the height and Width requests and the layout Option. The Layout Options Structure Encapsulates two layout preferences:

  • » Alignment: Determines the position and size of the views within the parent layout.
    » Expansion: It's Only Used by Stack Layout and Indicates if the view should use Extra Space, if any available.



These Layouts Preferences can be Applied to a View, Relative to its parent, by setting the Horizontal Option or Vertical Option property of the view to one of the public fields from the layout options structure.


Alignment:

The Alignment Controls how a view is positioned within its parent layout when the parent layout contains unused space. Following are the predefined Layout options available in Stack Layout.


OptionsDescription:
CenterCenter the view within the layout.
EndPlace the view at the end of the layout.
StartPlaces the View at the start of layout.
FillPlaces the View so that is has no padding.

In Stack Layout, we can set the layout option on the child views that are in opposite direction to the stack layout orientation. if the Layout is vertically Oriented, then we can set the Horizontally options properties of views to one of the start, center, end or fill fields. And if the Layout is Horizontally Oriented, then we set the vertically options properties of views to the start, center, end or fill fields.

The following XML code example demonstrates a Vertically Oriented StackLayout where each Views sets its Horizontal Options property to one of the four alignments fields.


<?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:Layout"
             x:Class="Layout.MainPage">

    <ContentPage.Content>
        
        <!--Layout Orientation Vertical-->
        <StackLayout Orientation="Vertical" Padding="20">
            <Button Text="Sizing Attribute"/>     
            
            <!--Views Sets its Horizontal Options property.-->
            <Label Text=" Start "  BackgroundColor="#008080" HorizontalOptions="Start"/>
            <Label Text=" Center " BackgroundColor="#008080" HorizontalOptions="Center"/>
            <Label Text=" End "    BackgroundColor="#008080" HorizontalOptions="End"/>
            <Label Text=" Fill "   BackgroundColor="#008080" HorizontalOptions="Fill"/>
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

Vertical Orientation & Horizontal Property : infobrother

The Following XML code Example demonstrates a Horizontally Oriented StackLayout where each views sets its Vertical Options property to one of the four alignments fields.


<?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:Layout"
             x:Class="Layout.MainPage">

    <ContentPage.Content>
        
        <!--Layout Orientation Horizontal-->
        <StackLayout Orientation="Horizontal" Padding="20">
            <Button Text="Sizing Attribute"/>     
            
            <!--Views Sets its Vertical Options property.-->
            <Label Text=" Start "  BackgroundColor="#008080" VerticalOptions="Start"/>
            <Label Text=" Center " BackgroundColor="#008080" VerticalOptions="Center"/>
            <Label Text=" End "    BackgroundColor="#008080" VerticalOptions="End"/>
            <Label Text=" Fill "   BackgroundColor="#008080" VerticalOptions="Fill"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Horizontal Orientation & Vertical Property : infobrother

The Following XML code Example demonstrates a Vertical Oriented StackLayout where each views sets its Vertical Options property to one of the four alignments fields.


NOTE: The Stack Layout Will Ignore the Layout Alignments Properties, if the views are in the same direction as the stack layout orientation.

<?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:Layout"
             x:Class="Layout.MainPage">

    <ContentPage.Content>
        
        <!--Layout Orientation Vertical-->
        <StackLayout Orientation="Vertical" Padding="20">
            <Button Text="Sizing Attribute"/>     
            
            <!--Views Sets its Vertical Options property.-->
            <Label Text=" Start "  BackgroundColor="#008080" VerticalOptions="Start"/>
            <Label Text=" Center " BackgroundColor="#008080" VerticalOptions="Center"/>
            <Label Text=" End "    BackgroundColor="#008080" VerticalOptions="End"/>
            <Label Text=" Fill "   BackgroundColor="#008080" VerticalOptions="Fill"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Vertical Orientation & Vertical Property : infobrother

Expansion:

Expansion Controls whether a view will Occupy more space, if Available, within a stack Layout. If the Stack Layout is larger than the combined size of all its children then the unused space is shared equally by all its child views. and this sharing space between views can happen by setting the Horizontal Options and Vertical Options properties to the layout Options field. Following are the Predefined layout options available in stack layout to expend the views.


OptionsDescription
CenterAndExpandCenter the view within the layout and expand it to take up as much space as the layout will give it.
EndAndExpandPlace the View at the end of the Layout (right or bottom) and expands it to take up as much space as the layout will give it.
FillAndExpandPlace the view so that it has no padding and expand it to take up as much space as the layout will give it.
StartAndExpandPlace the view at the start of the layout and expand it to take up as much space as the layout will give it.


In Stack Layout, the Views can only expand itself in the direction of its orientation. If the Layout is Vertically Oriented, then the views can expand their self by setting Vertically Options properties to one of the above given options.

The following XML code example demonstrates a Vertical Oriented StackLayout where each Views sets its Vertically Options property to one of the four Expansion fields.


<?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:Layout"
             x:Class="Layout.MainPage">

    <ContentPage.Content>
        
        <!--Layout Orientation Vertical-->
        <StackLayout Orientation="Vertical" Padding="20">
            <Button Text="Sizing Attribute"/>     
            
            <!--Views Sets its Vertical Options property.-->
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Start "  BackgroundColor="#008080" VerticalOptions="StartAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Center " BackgroundColor="#008080" VerticalOptions="CenterAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" End "    BackgroundColor="#008080" VerticalOptions="EndAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Fill "   BackgroundColor="#008080" VerticalOptions="FillAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Vertical Orientation & Vertical Property-Expansion : infobrother

The following XML code example demonstrates a Vertical Oriented StackLayout where each Views sets its Horizontal Options property to one of the four Expansion fields.


NOTE: The Stack Layout Will Ignore the Layout Expansion Properties, if the views are in the Opposite direction as the stack layout orientation.

<?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:Layout"
             x:Class="Layout.MainPage">

    <ContentPage.Content>
        
        <!--Layout Orientation Vertical-->
        <StackLayout Orientation="Vertical" Padding="20">
            <Button Text="Sizing Attribute"/>     
            
            <!--Views Sets its Horizontal Options property.-->
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Start "  BackgroundColor="#008080" HorizontalOptions="StartAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Center " BackgroundColor="#008080" HorizontalOptions="CenterAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" End "    BackgroundColor="#008080" HorizontalOptions="EndAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
            <Label Text=" Fill "   BackgroundColor="#008080" HorizontalOptions="FillAndExpand"/>
            <BoxView BackgroundColor="Red" HeightRequest="1" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Vertical Orientation & Horizontal Property-Expansion : infobrother

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement