【发布时间】:2020-11-14 12:09:27
【问题描述】:
正在为 Android 设备制作项目。
我已经搜索了一段时间,但找不到为什么会发生这种情况的答案。
<ListView x:Name="ScorebdList"
Grid.Row="0"
Grid.ColumnSpan="3"
Grid.RowSpan="4"
SelectionMode="None"
Header="HIGHSCORES">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Score}" HeightRequest="50"
MinimumHeightRequest="50" WidthRequest="50"
MinimumWidthRequest="50" HorizontalTextAlignment="Center"
BackgroundColor="Red"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
说明:
在这里,我试图设置标签的 height 和 width 和 Grid 的相同。
标签上的红色背景以查看它的去向。
这根本没有显示标签。
如果我这样做:
<ListView x:Name="ScorebdList"
Grid.Row="0"
Grid.ColumnSpan="3"
Grid.RowSpan="4"
SelectionMode="None"
Header="HIGHSCORES"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Score}" HorizontalTextAlignment="Center"
BackgroundColor="Red"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我添加了 HasUnevenRows 属性并将网格中的大小和宽度设置为自动。还删除了 Label 的高度/宽度请求。
现在标签会显示,但不是我想要的那样。
我测试的其他内容:
我还尝试将标签设置在 <StackLayout> 而不是 Grid 内,结果相同。
(在 <ViewCell> 内)
还尝试使用<TextCell> 而不是<ViewCell>,我无法居中但固定高度和宽度...
问题是:
为什么?我错过了什么?
使用第二个代码示例并不是我想要的样子。
我希望我的<ListView> 以我设置的高度和宽度在视图的中心显示高分。
(下面的代码与上面的代码颜色不同,标签为#D87040(橙色),网格背景为#321F1F(深红色)”) 这是两张图片,第一张图片的宽度/高度为 '*'。
【问题讨论】:
-
尝试设置 Grid 的高度值,而不是 Label。和/或设置 ListView 的 RowHeight
-
都试过了。问题是,当我这样做时,行得到了想要的正确高度,但标签完全消失了......并且没有显示分数。
标签: listview xamarin xamarin.forms xamarin.android