Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a button in Xamarin

I'm using Xamarin.Forms. I tried this code but it has not worked; how can I resize the buttons?

<ContentView.Content>
  <StackLayout>
    <Button Text="1" WidthRequest="50"></Button>
    <Button Text="2" WidthRequest="50"></Button>
  </StackLayout>
</ContentView.Content>

Thank you.

like image 321
tubefavorites.com Avatar asked Sep 29 '16 12:09

tubefavorites.com


4 Answers

<StackLayout >
    <Button Text="1" WidthRequest="50" 
            HorizontalOptions="Center"></Button>
    <Button Text="2" WidthRequest="50" 
            HorizontalOptions="Center"></Button>
</StackLayout>

enter image description here

like image 157
Vulcan Lee Avatar answered Nov 10 '22 23:11

Vulcan Lee


  <StackLayout>
    <!--I am wider but shorter-->
    <Button Text="1" WidthRequest="100" HeightRequest="50"></Button>
    <!--I am narrower but longer-->
    <Button Text="2" WidthRequest="50" HeightRequest="100"></Button>
    <!--I fill the whole width. See also VerticalOptions-->
    <Button Text="3" HorizontalOptions="FillAndExpand"></Button>
  </StackLayout>
like image 30
Link Ng Avatar answered Nov 10 '22 23:11

Link Ng


You can use HorizontalOptions attribute to make the width or height be as large as needed to contain the elements within it.

<Button  HorizontalOptions="Center"  Text="Retry" />
like image 3
Ali Besharati Avatar answered Nov 10 '22 23:11

Ali Besharati


Just Size parent View, for example if it's in a StackLayout, it'll be same size with parent.

   <Grid HeightRequest="120" WidthRequest="120" HorizontalOptions="Center">
            <Button Text="Hello!" BackgroundColor="Red"/>
        </Grid>

It'll be shown 120 x 120,

Because, Button's Default HorizontalOptions is Fill, VerticalOptions is Start. But some Controls like buttons, ignores Height or Width request and prefer to fill parent. Other elements in the same Layout effects button. Just resize parent of button and leave it.

like image 1
enisn Avatar answered Nov 10 '22 22:11

enisn