c# - 带有 HasUnevenRows 的 Listview 不会自动更改单元格高度?
问题描述
带有 HasUnevenRows 的 Listview 不会自动更改单元格高度,我的 listview 包含图像和标签,listview 行高不会随其内容而变化。我想用标签内容更改行高。
这是我的设计
<AbsoluteLayout AbsoluteLayout.LayoutBounds="0,1,1,0.57" AbsoluteLayout.LayoutFlags="All"
BackgroundColor="White" VerticalOptions="FillAndExpand">
<Image Source="waterMark" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
AbsoluteLayout.LayoutBounds="0,1,1,1" AbsoluteLayout.LayoutFlags="All" Margin="0,0,0,40"/>
<ListView x:Name="lv_List" IsPullToRefreshEnabled="True"
HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="None"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
Padding="0" Margin="0" BackgroundColor="WhiteSmoke">
<ci:CircleImage Source="profile" BorderColor="WhiteSmoke" AbsoluteLayout.LayoutBounds="0,0,0.3,1"
AbsoluteLayout.LayoutFlags="All" WidthRequest="65" HeightRequest="65"
HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" Aspect="AspectFill">
</ci:CircleImage>
<StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"
AbsoluteLayout.LayoutBounds="1,0,0.75,1" AbsoluteLayout.LayoutFlags="All">
<StackLayout.Margin>
<OnIdiom Phone="0,0,0,0"
Tablet="-90,0,0,0"/>
</StackLayout.Margin>
<Label Text="{Binding Name}" HorizontalOptions="FillAndExpand"
FontSize="17.5" TextColor="#555454"/>
<Label Text="{Binding Description}" HorizontalOptions="FillAndExpand"
FontSize="20" TextColor="#555454"/>
</StackLayout>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
解决方案
您可以Grid
在 listview 的 DataTemplate 中使用来实现这一点。有我的运行截图。
有布局。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App4"
xmlns:ci="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
x:Class="App4.MainPage">
<AbsoluteLayout AbsoluteLayout.LayoutBounds="0,1,1,0.57" AbsoluteLayout.LayoutFlags="All"
BackgroundColor="White" VerticalOptions="FillAndExpand">
<ListView x:Name="lv_List" IsPullToRefreshEnabled="True"
HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="Default"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<ci:CircleImage Source="profile" BorderColor="WhiteSmoke" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"
AbsoluteLayout.LayoutFlags="All" WidthRequest="65" HeightRequest="65"
HorizontalOptions="StartAndExpand"
VerticalOptions="CenterAndExpand" Aspect="AspectFill">
</ci:CircleImage>
<Label Text="{Binding Name}" HorizontalOptions="FillAndExpand" Grid.Row="0" Grid.Column="1"
FontSize="17.5" TextColor="#555454"/>
<Label Text="{Binding Description}" HorizontalOptions="FillAndExpand" Grid.Row="1" Grid.Column="1"
FontSize="20" TextColor="#555454"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
有listview的数据源。
public partial class MainPage : ContentPage
{
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
public MainPage()
{
InitializeComponent();
employees.Add(new Employee { Name = "Rob Finnerty", Description = "Rob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob Finnerty" });
employees.Add(new Employee { Name = "Bill Wrestler", Description = "Bill WrestlerBill Wrestler" });
employees.Add(new Employee { Name = "Dr. Geri-Beth Hooper", Description = "Dr. Geri-Beth Hooper" });
employees.Add(new Employee { Name = "Dr. Keith Joyce-Purdy", Description = "Dr. Keith Joyce-PurdyDr. Keith Joyce-PurdyDr. Keith Joyce-Purdy" });
employees.Add(new Employee { Name = "Sheri Spruce", Description = "Sheri SpruceSheri Spruce" });
employees.Add(new Employee { Name = "Burt Indybrick", Description = "Burt IndybrickBurt Indybrick" });
lv_List.ItemsSource = employees;
}
}
有型号。
public class Employee
{
public string Name { get; set; }
public string Description { get; set; }
}
推荐阅读
- php - RegEx - 匹配以冒号开头的子字符串
- php - 使用 PHP 从 XML 中删除与节点中的特定字符串匹配的所有元素
- ionic-framework - 离子本地通知错误:未捕获(承诺):TypeError:Object(...)不是函数
- azure - 从 docker compose 移植的 Azure IoT Edge 清单
- c# - 如何修复容易发生 SQL 注入的内联 C# 查询
- python - 删除列表中字符串的最后一个特殊字符
- c++ - 在 C/C++ 中签名扩展数字的最佳方法是什么
- xcode - 快速将启动画面添加到可可应用程序
- node.js - 如何在launch.json的Visual Studio Code中反转$ {relativeFile}中的反斜杠?
- javascript - jQuery:隐藏功能无法正常工作?