Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms StackLayout: How to set width / height of children to percentages

Is there a way to create a vertical stack layout with a button that takes 30% of of the parent, and a text input that takes 70% of the parent? Something like this:

<StackLayout Orientation="Vertical">     <Entry Height="70%"></Entry>     <Button Height="30%">Click me</Button> </StackLayout> 

But this doesn't work. Only solution so far is creating a complete Grid item and using that. Are there no other solutions?

like image 564
sgarcia.dev Avatar asked May 14 '15 22:05

sgarcia.dev


People also ask

What is stack layout?

A StackLayout organizes child views in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout is oriented vertically. In addition, a StackLayout can be used as a parent layout that contains other child layouts.

What is Flex layout in xamarin forms?

The Xamarin. Forms FlexLayout is new in Xamarin. Forms version 3.0. It is based on the CSS Flexible Box Layout Module, commonly known as flex layout or flex-box, so called because it includes many flexible options to arrange children within the layout. FlexLayout is similar to the Xamarin.

Which LayoutOptions value would you use to request and fill extra space in a StackLayout?

The StartAndExpand , CenterAndExpand , EndAndExpand , and FillAndExpand values are used to define the alignment preference, and whether the view will occupy more space if available within the parent StackLayout . The default value of a view's HorizontalOptions and VerticalOptions properties is LayoutOptions. Fill .


1 Answers

StackLayout doesn't go very well with varying heights in this scenario. The Xamarin Forms engines isn't as well rounded as the WPF engine at this point. Basically you have to go

<Grid>     <Grid.RowDefinitions>         <RowDefinition Height="7*" />         <RowDefinition Height="3*" />     </Grid.RowDefinitions>     <Entry Grid.Row="0" VerticalOptions="Fill"></Entry>     <Button Grid.Row="1" VerticalOptions="Fill">Click me</Button> </Grid> 

Also as a side note, only Grids can expand to the full width or height of its parent element. StackLayout's determine their maximum size by the sum of their children unless they are within a Grid and you put HorizontalOptions="Fill" and VerticalOptions="Fill".

The only other way you could accomplish this is to get the DeviceHeight and Width from the OS and manually set the Heights of your elements. A complex and usually flawed approach, which I don't recommend.

like image 112
Adam Avatar answered Sep 24 '22 13:09

Adam