Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overlay elements with Xamarin.Forms

I simply want to create an Autocomplete element on the top where the suggestion list is not shifting down the remaining elements im my Layout.

I could try to wrap the Entry and ListView in an AbsoluteLayout container, but for me this sounds not like a clean solution. Is there a different way to achieve this?

like image 839
core Avatar asked Jul 30 '15 12:07

core


Video Answer


3 Answers

Been using Grid all the time until it started randomly crashing with complicated layouts.

Anyway the solution is to use an AbsoluteLayout. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/absolutelayout

Example of 2 centered overlayed items:

    <AbsoluteLayout 
                 VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        
     <BoxView
                       AbsoluteLayout.LayoutBounds="0.5, 0.5, -1, -1" 
                AbsoluteLayout.LayoutFlags="PositionProportional"          
              BackgroundColor="Red"
              HeightRequest="100"
              WidthRequest="100"/>
    
     <ActivityIndicator
                       AbsoluteLayout.LayoutBounds="0.5, 0.5, -1, -1" 
                AbsoluteLayout.LayoutFlags="PositionProportional"          
              IsRunning="True"
              VerticalOptions="CenterAndExpand"
              HorizontalOptions="CenterAndExpand"/>
</AbsoluteLayout>

XAML Tips:

FillAndExpand XY:

AbsoluteLayout.LayoutBounds="0, 0, 1, 1" 
AbsoluteLayout.LayoutFlags="All"

Center XY:

AbsoluteLayout.LayoutBounds="0.5, 0.5, -1, -1"     
AbsoluteLayout.LayoutFlags="PositionProportional"

Vertical Center, Horizontal Fill:

AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, -1" 
AbsoluteLayout.LayoutFlags="PositionProportional"

Vertical End, Horizontal Center

AbsoluteLayout.LayoutBounds="0.5, 1, -1, -1" 
AbsoluteLayout.LayoutFlags="PositionProportional"

Vertical Start, Horizontal Center

AbsoluteLayout.LayoutBounds="0.5, 0, -1, -1" 
AbsoluteLayout.LayoutFlags="PositionProportional"

Vertical Start, Horizontal Start

AbsoluteLayout.LayoutBounds="0, 0, -1, -1" 
AbsoluteLayout.LayoutFlags="PositionProportional"
like image 165
Nick Kovalsky Avatar answered Oct 27 '22 15:10

Nick Kovalsky


If you use a Grid you can overlay stuff and set if should go left, right, center etc.. The last one to be added will be on top, in your case Entry.

like image 44
Rui Marinho Avatar answered Oct 27 '22 14:10

Rui Marinho


In order to overlay elements you can use "Absolute" and/or "Relative" Layout.

There is workaround for overlaying using "Grid". In order to do that you need to put your elements or "containers" in same Row/Cell.

Here is example:

<Grid>
    <Label Grid.Row="0" Text="test" VerticalTextAlignment="Center" />
    <Label Grid.Row="0" Text="overlay" VerticalTextAlignment="Center" />
</Grid>
like image 27
Ljupcho Hristov Avatar answered Oct 27 '22 13:10

Ljupcho Hristov