Center icon and text in Xamarin XAML button
I can't center the icon inside the button and get it over the text, I didn't find any information about this either, I was asked to make a kind of "tabb" options menu, but below and I found it to be "difficult" due to "design implications" etc., so I figured it would be easier with buttons, but now the problem is how to center the image and get the text correctly. If some I could help me that would be great. This is my code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
BackgroundColor="Silver"
x:Class="XamForm.View.MainPage">
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
<StackLayout Padding="30">
<Label x:Name="lblCuantos" Text="No #" TextColor="Gray" FontSize="20"/>
<Label x:Name="lblNombre" Text="" FontSize="20"/>
<Button x:Name="btn1" Text="Oprimir" Clicked="Accion"/>
<Button x:Name="btn2" BackgroundColor="White" Image="iconRes.png" HorizontalOptions="CenterAndExpand" Text="Gridd" TextColor="Gray" Clicked="Accion2"/>
<Button x:Name="btn3" BackgroundColor="White" Image="iconRes.png" HorizontalOptions="CenterAndExpand" TextColor="Gray" Clicked="Accion2"/>
</StackLayout>
</ContentPage>
This is the actual result of the button itself:
http://oi59.tinypic.com/11so9le.jpg
This is what the end result of the menu should look like kind
http://oi62.tinypic.com/10pzw2f.jpg
Thank!
source to share
You can use a button ContentLayout="Top,0"
within a button. For example, in your case use below code
<Button x:Name="btn2" ContentLayout="Top,0" BackgroundColor="White" Image="iconRes.png" HorizontalOptions="CenterAndExpand" Text="Gridd" TextColor="Gray" Clicked="Accion2" />
<Button x:Name="btn3" ContentLayout="Top,0" BackgroundColor="White" Image="iconRes.png" HorizontalOptions="CenterAndExpand" TextColor="Gray" Clicked="Accion2"/>
source to share
The Button.Image property is of type FileImageSource, so any image that is "local" can be used by the button. This means that the following code will work:
someButton.Image = "imageName.png";
or you can write
someButton.Image = ImageSource.FromFile("imageName.png");
if imageName.png is in each of the application projects (Resources folder on iOS, Resources / Driver folder on Android, and application root on WinPhone, each with a corresponding set of build actions).
Xaml equivalent:
<Button Text="Has Image" Image="someImage.png" />
Refer to the Working with Images doc for complete details on what needs to be done for each platform.
source to share