How can I set the width of an image inside a button in Xamarine.Forms xaml?

I would like to know if it is possible to set the width and height of the image inside this button:

<Button Image="ic_music_white.png" BorderWidth="1" BorderColor="White" HeightRequest="56"  BackgroundColor="PowderBlue" HorizontalOptions="Center" Clicked="Button_Clicked">
</Button>

      

Any solution?

+6


source to share


5 answers


You can always create an Image control and add a gesture recognizer to it. This way you have more control over the size and placement of the image.

XAML

<Image x:Name="myImage" Source="ic_music_white.png" HeightRequest="56"  BackgroundColor="PowderBlue" HorizontalOptions="Center"/>

      



XAML.CS

TapGestureRecognizer tapEvent = new TapGestureRecognizer();
tapEvent.Tapped += Button_Clicked;
myImage.GestureRecognizers.Add(tapEvent);

      

+5


source


This is not possible from a Xamarin.Forms project, but you can create your own renderers that will allow you to change the properties of an embedded control.

For iOS, you will be modifying ImageEdgeInsets .



For Android, you can take a look at Button renderer so you can get any ideas.

Hope it helps.

+1


source


In an iOS project, padding can adjust the size of the image inside the button. If you have a width of 50 with a padding of 10, the resulting image width will be 30.

+1


source


No, you cannot control the height / width of the image inside button

. The best option would be to create a stack layout and then put text and image in it. How I did it:

<StackLayout Grid.Row="2" Orientation="Horizontal" HorizontalOptions="FillAndExpand"  VerticalOptions="End" HeightRequest="60">

                <Label Text="Review task" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand" FontSize="25" FontAttributes="Bold"/>
                <Image Source="nextarrow_white.png" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" HeightRequest="32" Margin="20,4,0,0"/>

        </StackLayout>

      

0


source


You missed adding WidthRequest. Can you add WidthRequest like below

<Button Image="ic_music_white.png" BorderWidth="1" BorderColor="White" HeightRequest="56" WidthRequest="56"  BackgroundColor="PowderBlue" HorizontalOptions="Center" Clicked="Button_Clicked">
</Button>

      

-2


source







All Articles