Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms stacklayout fill screen on landscape

How can I do stacklayout take all screen width when device is in Landscape? in Landscape mode stacklayout does not fill parent

here is my Xaml:

<?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:ListView"
         x:Class="ListView.MainPage">
<ContentPage.Content>
        <AbsoluteLayout BackgroundColor="Silver" HorizontalOptions="FillAndExpand">
        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" BackgroundColor="Maroon" VerticalOptions="StartAndExpand">
            <Button Text="Reload data" Clicked="reloadData" x:Name="btnReload"/>
            <ListView x:Name="listView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout BackgroundColor="#eee" Orientation="Vertical">
                                <StackLayout Orientation="Horizontal"  VerticalOptions="FillAndExpand">
                                    <Image Source="{Binding Imagen}" HeightRequest="100" WidthRequest="120"/>
                                    <Label Text="{Binding Titulo}" TextColor="Gray"/>
                                    <Label Text="{Binding Fecha}"  HorizontalOptions="EndAndExpand"/>
                                </StackLayout>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
        <ActivityIndicator BackgroundColor="Green" AbsoluteLayout.LayoutBounds=".5,.5,.2,.2" AbsoluteLayout.LayoutFlags="All" Color="Blue"
                            IsRunning="True" IsVisible="False" x:Name="overlay"/>
    </AbsoluteLayout>
</ContentPage.Content>

I want stacklayout take screen width on landscape; but only on portrait is work.

like image 996
Eric Ocampo Avatar asked Mar 11 '23 13:03

Eric Ocampo


1 Answers

When controls are direct children of an AbsoluteLayout setting HorizontalOptions and VerticalOptions has no effect. All of the sizing must come from setting AbsoluteLayout.LayoutBounds and AbsoluteLayout.LayoutFlags. So try changing your StackLayout to this:

<AbsoluteLayout BackgroundColor="Silver"
                HorizontalOptions="FillAndExpand">
  <StackLayout Orientation="Vertical" 
               BackgroundColor="Maroon"
               AbsoluteLayout.LayoutBounds="0,0,1,1"
               AbsoluteLayout.LayoutFlags="All">
    ...

Then you also may need to set your ListView.HorizontalOptions to FillAndExpand but try without that first.

like image 172
hvaughan3 Avatar answered Mar 25 '23 06:03

hvaughan3