I am trying to make two labels each taking 50% of the width of the parent (the StackLayout) I tried with Fill, FillAndExpand, StartAndExpand on both Label's but it didn't work:
<StackLayout Orientation="Horizontal">
<Label Text="AAAAAAAAAAAAAAAAAAAAAAAAA" HorizontalOptions="FillAndExpand" />
<Label Text="BBBB" HorizontalOptions="FillAndExpand" />
</StackLayout>
Use a Grid
instead.
XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Text="AAAAAAAAAAAAAAAAAAAAAAAAA" Grid.Column="0"/>
<Label Text="BBBB" Grid.Column="1"/>
</Grid>
The two *
s mean the columns will have the same width. If you wanted the first column to be twice has big as the second, you could change the first width to 2*
.
You can also use EndAndExpand
to have the text right-aligned
I know this question was asked last year. But I hope my answer will help other.
Actually with StackLayout
you can get the center and cut in half. I love using StackLayout
because it has it's own automatic sizing, if you put 3 Views
in a StackLayout.Children
, the StackLayout
will divide these 3 columns at the same exact size.
Here's how I do it in C#:
Content = new StackLayout{
Orientation = StackOrientation.Horizontal, //Set the StackLayout to start from left to the right
HorizontalOptions = LayoutOptions.CenterAndExpand, //Expand the layout to full width and center it
Children = {
new Label{
Text = "Test One"//you may add HorizontalOptions in here to if you want the text on the left or right
},
new Label{
Text = "Text Two"
}
}
};
Here's how I do it in XAML:
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Label Text="Test One"></Label>
<Label Text="Text Two"></Label>
</StackLayout>
I hope this would answer the question on StackLayout
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With