xaml - Xamarin Grid 不均匀拆分
问题描述
我希望 ListView DataTemplate Cell 中包含的两个标签在屏幕上水平均匀分布。
据我了解,如果您使用 Grid 并将两者设置ColumnDefinitions
为1*
它应该可以工作。我试过了,它没有按预期显示。
我必须在第一个标签上添加一个大WidthRequest
标签才能让它工作。
我还尝试将HorizonalOptions
Grid 和标签设置为FillAndExpand
,但没有结果。
<ViewCell>
<Frame HasShadow="true" Margin="2">
<StackLayout Orientation="Horizontal" Margin="0,0,0,0" >
<StackLayout WidthRequest="20" BackgroundColor="{Binding RYGStatusColor}" >
<Label Text="{Binding RYGStatusLetter}" HorizontalTextAlignment="Center"
FontSize="Medium" FontAttributes="Bold" TextColor="White" />
</StackLayout>
<StackLayout Orientation="Vertical">
<Label Text="{Binding ProgramName}" FontSize="Medium" FontAttributes="Bold" />
<Label Text="{Binding DesignCustomerName}" FontSize="Small" />
<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"
Text="{Binding EstimatedTotalValue, Converter={StaticResource CurrencyDisplay}}"
FontSize="Small" FontAttributes="Bold" WidthRequest="1024" />
<!-- WidthRequest is a hack to force the width to be equal across the screen. -->
<Label Grid.Row="0" Grid.Column="1" FontSize="Small">
<Label.FormattedText>
<FormattedString>
<Span Text="Modified: " />
<Span Text="{Binding ModifiedDate, StringFormat='{0:M/d/yy}', Converter={StaticResource LocalTime}}}" />
</FormattedString>
</Label.FormattedText>
</Label>
</Grid>
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
如果没有WidthRequest
hack,我会得到以下结果:实际结果
第 1 行 “标签1 标签2” 第 2 行 “标签1 标签2”
通过 hack,我得到了预期的结果:预期结果
第 1 行 “标签1 标签2” 第 2 行 “标签1 标签2”
解决方案
我希望 ListView DataTemplate Cell 中包含的两个标签在屏幕上水平均匀分布。据我了解,如果您使用 Grid 并将两个 ColumnDefinitions 设置为 1* 它应该可以工作。我试过了,它没有按预期显示。
我在 ListView 日期模板中使用您的代码,并且不使用 widthrequest,我得到以下结果。Xamarin.Form 版本是 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>
推荐阅读
- android - 如何使 TextInputLayout 的底角变圆
- google-ads-api - 如何在 Google Ad Manager 中获取预定义报告(维度)扩展调用详细信息报告的下划线 GAQL 查询?
- authentication - Vmware 身份设备的问题
- matplotlib - matplotlib - 以十六进制格式显示图像颜色
- javascript - 为什么 Java 脚本给出错误:无法读取未定义的属性“名称”
- java - 尝试将子级添加到通用树时出现 Java 堆栈溢出错误
- clickhouse - 有没有更好的方法来跨 clickhouse 集群查询系统表?
- excel - 结束块 VBA 消息
- entity-framework-core - 为什么 EF Cores CodeFirst 不尊重 HasPrecision?
- reactjs - 如何使用 React Redux Hooks 加载微调器