Xaml Xamarin栅格不均匀分割
我希望ListView DataTemplate单元格中包含的两个标签在屏幕上水平均匀地分开 据我了解,, 如果使用网格并将两个Xaml Xamarin栅格不均匀分割,xaml,listview,xamarin,xamarin.forms,grid,Xaml,Listview,Xamarin,Xamarin.forms,Grid,我希望ListView DataTemplate单元格中包含的两个标签在屏幕上水平均匀地分开 据我了解,, 如果使用网格并将两个列定义设置为1*,则应能正常工作。我尝试了这个,但它没有按预期显示 我必须在第一个标签上添加一个大的WidthRequest,才能让它工作 我还尝试将网格和标签上的水平选项设置为FillAndExpand,但没有结果 如果没有WidthRequesthack,我会得到以下结果: 一排 “Label1 Label2” 第2排 “Label1 Label2” 通过黑客攻
列定义设置为1*
,则应能正常工作。我尝试了这个,但它没有按预期显示
我必须在第一个标签上添加一个大的WidthRequest
,才能让它工作
我还尝试将网格和标签上的水平选项设置为FillAndExpand
,但没有结果
如果没有WidthRequest
hack,我会得到以下结果:
一排
“Label1 Label2”
第2排
“Label1 Label2”
通过黑客攻击,我得到了预期的结果:
一排
“Label1 Label2”
第2排
“Label1 Label2”
我怀疑网格正在正确地自行拆分,但它没有占用您所期望的全部空间。尝试将HorizontalOptions=“FillAndExpand”
添加到网格本身并报告
我希望ListView DataTemplate单元格中包含的两个标签在屏幕上均匀地水平分割。据我所知,如果使用网格并将两个ColumnDefinition设置为1*,应该可以工作。我尝试了这个,但它没有按预期显示
我在ListView datetemplate中使用您的代码,而不使用widthrequest,我得到以下结果。表单版本是3.4.0.1008975
代码如下:
<ContentPage.Content>
<ListView ItemsSource="{Binding models}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
BackgroundColor="Blue"
FontAttributes="Bold"
FontSize="Small"
Text="{Binding str1}" />
<!-- WidthRequest is a hack to force the width to be equal across the screen. -->
<Label
Grid.Row="0"
Grid.Column="1"
BackgroundColor="Yellow"
FontSize="Small">
<Label.FormattedText>
<FormattedString>
<Span Text="Modified: " />
<Span Text="{Binding str2}" />
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
尝试在网格和标签上设置背景色-这将有助于澄清不同元素实际占用的空间,以及您是否需要调整布局选项谢谢,我懒得上传屏幕截图,但这样做了,您可以看到结果,与我的模型相同。这只是ViewCell布局的一部分吗?我认为看到整个布局会有所帮助。你的屏幕截图显示的内容比你的XAML代码片段多@Jason。更新到完整视图单元已将HorizontalOptions=FillAndExpand添加到包含的StackLayout中,并修复了该问题。谢谢大家!上面Jason在整个ViewCell上的注释和您的评论促使我考虑包含StackLayout的内容。添加了horizontalpoptions=FillAndExpand,并将其修复。谢谢
Row 1
"Label1 Label2 "
Row 2
"Label1 Label2 "
<ContentPage.Content>
<ListView ItemsSource="{Binding models}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
BackgroundColor="Blue"
FontAttributes="Bold"
FontSize="Small"
Text="{Binding str1}" />
<!-- WidthRequest is a hack to force the width to be equal across the screen. -->
<Label
Grid.Row="0"
Grid.Column="1"
BackgroundColor="Yellow"
FontSize="Small">
<Label.FormattedText>
<FormattedString>
<Span Text="Modified: " />
<Span Text="{Binding str2}" />
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>