Xamarin Forms Button taking up whole screen despite relative AbsoluteLayout LayoutBounds settings

Owen Burns 96 Reputation points
2021-07-07T17:54:43.86+00:00

I am attempting to add a button to an absolutelayout using relative LayoutBounds so that it doesn't take up the whole screen. However, the button is taking up the whole screen. My c# code is as follows:

Button temp = new Button()  
{  
     Text = "Test"  
};  
AbsoluteLayout.SetLayoutBounds(temp, new Xamarin.Forms.Rectangle(.1, .1, .1, .1));  
AbsoluteLayout.SetLayoutFlags(temp, AbsoluteLayoutFlags.All);  
photoGrid.Children.Add(temp);  

photoGrid is my absoslutelayout. I am following the tutorial here (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/absolutelayout#proportional-positioning-and-sizing) and see no reason why the button would be taking up the entire screen instead of a small part of it (10% away from the left and 10% away from the top with dimensions taking up 10% of the screen in each direction).

The AbsoluteLayout also has an image inside it by the time that this button is added; the XAML code is as follows:

<AbsoluteLayout x:Name="PhotoGrid" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsVisible="False">  
     <Image x:Name="PhotoImage" AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All" Aspect="AspectFit"/>  
</AbsoluteLayout>  

Finally, the AbsoluteLayout itself is inside a 1 row, 1 column grid. This is because the content of the page changes as the user uses the app, and placing several views inside a grid like this allows me to change which of them is visible at a given time depending on what the app is doing.

Why might the button generated with the c# code above take up the entirety of the full page gridview, and how can I fix this?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,284 questions
{count} votes

Accepted answer
  1. Owen Burns 96 Reputation points
    2021-07-08T12:44:04.663+00:00

    Hi! I discovered that the issue was variable names. I had somehow created a grid called photoGrid and an AbsoluteLayout called PhotoGrid and was incorrectly adding the button to the former instead of the latter.

    0 comments No comments

0 additional answers

Sort by: Most helpful