You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在FMX的TListView项中添加图片?已关联ImageList仍异常

ListView关联ImageList后图片不显示的排查方案
  • 检查ImageList配置
    • 确认ImageList1中确实存在索引为0的图片,设计时打开ImageList编辑器验证,确保图片未被误删或未正确添加。
    • 匹配ImageList尺寸与ListView的图标模式:若ListView使用大图标模式,ImageList的宽高需对应设置,否则图片可能无法显示或被截断。
  • 确认ListView视图模式
    • 部分视图模式不支持显示图片,比如lvList模式仅显示文本。需将ListView的ViewStyle设置为支持图标的模式,例如lvIcon、lvSmallIcon或lvDetails(Details模式需额外设置对应列的ImageIndex)。
  • 优化代码逻辑
    • 将ListView1.Images := ImageList1;移至初始化环节(如FormCreate),避免每次查询重复赋值引发控件状态异常。
    • 设置Item.ImageIndex后,可调用ListView1.Repaint;强制刷新控件,确保图片渲染。
  • 适配FireMonkey环境(若使用FMX)
    • 若为FMX的TListView,需确保ListView的ItemAppearance使用带图片的样式(如ImageListItemBottomDetail),仅设置Item.ImageIndex可能无法显示图片。

原代码参考

var
  Item: TListViewItem;
begin
  UniQuery1.SQL.Text := 'SELECT * FROM Clients WHERE Problem IS NOT NULL';
  UniQuery1.Open;
  ListView1.Items.Clear;
  if not UniQuery1.IsEmpty then
  begin
    ListView1.Images := ImageList1;
    while not UniQuery1.Eof do
    begin
      Item := ListView1.Items.Add;
      Item.Text := 'Name: ' + UniQuery1.FieldByName('Name').AsString;
      Item.Detail := 'Number: ' + UniQuery1.FieldByName('Number').AsString +
                     ' | Problem: ' + UniQuery1.FieldByName('Problem').AsString;

      Item.ImageIndex := 0;
      UniQuery1.Next;
    end;
  end;
  UniQuery1.Close;
end;

内容的提问来源于stack exchange,提问作者ARTHUR LEMES

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 06:57:07