Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Xamarin forms' Button.ContentLayout property?

Using the latest pre-release, I noticed that the button now has a Button.ContentLayout property, which I am hoping will allow us to add custom views to buttons whilst retaining the rest of the buttons functionality.

The questions are, is this what this is for? And if so, how is it used?

Kind Regards

Brian

like image 637
SacredGeometry Avatar asked Nov 14 '16 11:11

SacredGeometry


1 Answers

No, this property does not allow you to set any custom content to be rendered inside the button.

ContentLayout on the Button element is a property of type "ButtonLayoutContent" it determines the positioning of the button's image in relation to it's text. It has two properties, image position and spacing.

Position is used to set the placement of the image in relation to the text. The image can be above or below the text, or to the left or the right side of the text.

Spacing is the amount of space between the image and the text. In the Android implementation of the Button renderer, it sets the CompoundDrawablePadding property, which is defined as the padding between the compound drawables and the text.

On iOS, the default renderer does some math to figure out the correct values for ImageEdgeInsets, TitleEdgeInsets and ContentEdgeInsets

Example usage in XAML:

<Button BackgroundColor="Color.Gray" Image="coffee.png" Text="Click Me" ContentLayout="Top,10">

In C# code, you simply pass the two values in the constructor

btn.ContentLayout = new ButtonContentLayout(ImagePosition.Top,10);
like image 58
irreal Avatar answered Oct 27 '22 08:10

irreal