Stacking of Layouts #20676
-
Hi there, I would like to ask a question about stacking of layouts. I am not talking about a StackLayout which stacks the elements underneath horizontally/vertically. What is mean is stacking elements on top of each other so that I can have a Background Image underneath, a LinearGradient on top of that, other elements on top of the Gradient, e.g. another Grid which is transparent and contains the elements on top of all that. The Grid containing a Border in there which visually contains other elements. Hope, I could explain correctly. I want to stack all elements on top of each other that's it. What I used to do so far was something like: <Grid RowDefinitions="Auto">
<!-- Container for Image and LinearGradient -->
<Grid Grid.Row="0">
<!-- Image at the bottom -->
<Image x:Name="BackgroundImage" Source="image.jpg" Aspect="AspectFill"/>
<!-- LinearGradient on top -->
<Grid>
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Transparent" Offset="1.0" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>
<!-- Needs to be down there, otherwise the Border is not on top of the Image and LinearGradient -->
<Grid Grid.Row="0" RowDefinitions="*" Grid.RowSpan="2">
<Border x:Name="HelpPageBorder" Grid.Row="0" VerticalOptions="Start" Style="{StaticResource BorderStyleView}" />
<VerticalStackLayout Grid.Row="0" Margin="10, 35, 0, 10">
<Label Text="Welcome to Help!" VerticalOptions="Center" HorizontalOptions="Center" />
<searchBarControl:SearchBarLang ios:SearchBar.SearchBarStyle="Minimal" Margin="0, 0, 0, 5" Placeholder="DASFDSDDSFSD" Text="{Binding SearchText}" CancelButtonText="{loc:Translate CancelButtonText}"/>
</VerticalStackLayout>
</Grid>
</Grid> Problem here is actually the need to be in the same Or would you try and do it all with ZIndex here? What do you think is cleanest to layout and would be the preferred way? Downsides of methods? <Grid>
<!-- Image at the bottom -->
<Image x:Name="BackgroundImage" ZIndex="1" Source="help_page.jpg" Aspect="AspectFill"/>
<!-- LinearGradient on top -->
<Grid ZIndex="2">
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="White" Offset="0.0" />
<GradientStop Color="Transparent" Offset="1.0" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
<Grid ZIndex="3" RowDefinitions="*">
<Border x:Name="HelpPageBorder" VerticalOptions="FillAndExpand" Style="{StaticResource BorderStyleView}" />
</Grid>
<VerticalStackLayout ZIndex="4" Margin="15, 15, 15, 10">
<searchBarControl:SearchBarLang ios:SearchBar.SearchBarStyle="Minimal" Margin="0, 0, 0, 5" Placeholder="{loc:Translate HelpPage_SearchBarText}" Text="{Binding SearchText}" CancelButtonText="{loc:Translate CancelButtonText}"/>
</VerticalStackLayout>
</Grid> I wanted to ask if there are better ways to achieve such a kind of stacking which is cleaner? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I remember a few years ago there was talk of a ZStackLayout which would've done exactly this, but I can't find the issue. |
Beta Was this translation helpful? Give feedback.
-
In my opinion, I would avoid using ZIndex unless I needed to explicitly override a ZIndex that didn't match where the view is in the XAML structure. If you want to display views on top of other views, you probably want AbsoluteLayout. Grid can do a limited amount of this, as you're already using, but AbsoluteLayout gives you a bit more control on how to layout everything. <Border>
<AbsoluteLayout>
<Image
x:Name="BackgroundImage"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1"
Aspect="AspectFill" />
<Grid
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1">
<Grid.Background />
<!-- Implement UI -->
<searchBarControl:SearchBarLang />
<!-- etc. -->
</Grid>
</AbsoluteLayout>
</Border> In detail:
Say you wanted to float a centered title or something as well. You could use: <Label
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds="0.5,0.25"/> would position the label at 1/2 of the AbsoluteLayout's width and 1/4 of the AbsoluteLayout's height (from top), then let the Label decide its own actual width and height. |
Beta Was this translation helpful? Give feedback.
I remember a few years ago there was talk of a ZStackLayout which would've done exactly this, but I can't find the issue.
I would guess that using ZIndex would be the way to accomplish this now.