[英]Xamarin.Forms layout issue
I am trying to produce a simple form within Xamarin.Forms that will display a ListView with a custom header (allowing data entry) and a custom footer (containing "Cancel"/"Continue" buttons). 我试图在Xamarin.Forms中生成一个简单的表单,该表单将显示具有自定义页眉(允许输入数据)和自定义页脚(包含“取消” /“继续”按钮)的ListView。 I cannot however get the ListView to fill the form vertically (I want the "Cancel"/"Continue" buttons at the bottom of the form, irrespective of the length of the list.
但是,我无法让ListView垂直填充表单(无论列表的长度如何,我都希望在表单底部使用“取消” /“继续”按钮。
Where I am going wrong? 我要去哪里错了? I have tried nearly every combination of VerticalOptions against the ListView but I always end up with the "Cancel"/"Continue" buttons sitting directly under the "OK" button...
我已经对ListView尝试了VerticalOptions的几乎每种组合,但是我总是以直接位于“ OK”按钮下方的“ Cancel” /“ Continue”按钮结束。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MyCompany.Views.MyPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Title="{Binding Path=Title}">
<ListView Margin="15" VerticalOptions="FillAndExpand">
<ListView.HeaderTemplate>
<DataTemplate>
<StackLayout VerticalOptions="Start">
<Label HorizontalTextAlignment="Center" Text="Type something" />
<Entry />
<Button HorizontalOptions="Center" Text="Ok" />
</StackLayout>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.FooterTemplate>
<DataTemplate>
<Grid VerticalOptions="End">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="1" Text="Cancel" />
<Button Grid.Column="3" Text="Complete" />
</Grid>
</DataTemplate>
</ListView.FooterTemplate>
</ListView>
</ContentPage>
For the record, the application is running (currently) on Android though I do not expect this to make any difference. 作为记录,该应用程序正在(当前)在Android上运行,尽管我认为这不会有任何不同。
I can't quite get it going either using the Header and Footer. 我不能完全使用页眉和页脚来实现它。 Alternatively you could also just wrap it in a Grid like so:
另外,您也可以将其包装在Grid中,如下所示:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" VerticalOptions="Start">
<Label HorizontalTextAlignment="Center" Text="Type something" />
<Entry />
<Button HorizontalOptions="Center" Text="Ok" />
</StackLayout>
<ListView Grid.Row="1" ItemsSource="{Binding .}" Margin="15" VerticalOptions="FillAndExpand" />
<Grid Grid.Row="2" VerticalOptions="End" HeightRequest="40">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="1" Text="Cancel" />
<Button Grid.Column="3" Text="Complete" />
</Grid>
</Grid>
UPDATE: The behavior I've noticed with ListView
is that it puts the footer directly after the last item in the ListView
. 更新:我已经注意到与行为
ListView
是,它使得页脚中的最后一个项目之后直接ListView
。 So if your ListView
has only 4 items it puts the header directly after it. 因此,如果您的
ListView
只有4个项目,它将标头直接放在它之后。 Try giving the footer and the ListView
a background color and you'll probably see something like this: 尝试为页脚和
ListView
提供背景颜色,您可能会看到类似以下的内容:
I don't think this behaviour can be altered without going the Grid route I suggested. 我认为如果不遵循我建议的网格路线,就无法改变这种行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.