【问题标题】:Text Alignment of ListViewItemListViewItem 的文本对齐方式
【发布时间】:2021-03-04 08:46:47
【问题描述】:

在 Delphi 10.4 的 Firemonkey 中。我想构建一个列表视图,其中列表视图项文本显示为左对齐或右对齐,具体取决于列表项内容。

我这样做如下:

procedure TForm1.FormCreate(Sender: TObject);

  procedure AddItem(No: integer);
  var
    Item: TListViewItem;
  begin
    Item := ListView1.Items.AddItem(No);
    Item.Text := 'Text item No ' + No.ToString;
    Item.Tag := No;
    if No mod 2 = 0 then
      Item.Objects.TextObject.TextAlign := TTextAlign.Leading
    else
      Item.Objects.TextObject.TextAlign := TTextAlign.Trailing;
  end;

var
  c: integer;
begin
  for c := 0 to 9 do
    AddItem(c);
end;

但是,这并不适用于所有平台。但是一旦 ListView 改变了它的大小,所有的 ListItems 就会在左侧齐平显示。有没有更好的方法来做到这一点?

作为一种解决方法,我做了以下操作:

procedure TForm1.ListView1Resized(Sender: TObject);
var
  c: integer;
begin
  for c := 0 to ListView1.ItemCount - 1  do
    if ListView1.Items[c].Tag mod 2 = 0 then
      ListView1.Items[c].Objects.TextObject.TextAlign := TTextAlign.Leading
    else
      ListView1.Items[c].Objects.TextObject.TextAlign := TTextAlign.Trailing;
end;

【问题讨论】:

  • 我认为最好完全自定义 ListViewItem。您可以放置​​ TLayout、TRectangle 或 TText 并做您想做的事情,结果在所有板块上总是相同的
  • 看起来像 2 人聊天。当您只想要一个没有标题的列时,为什么还要使用TListViewTRichEdit 不是更好的选择,因为它还允许用户选择部分文本以将其复制到剪贴板?
  • 是的,我构建了一个简单的聊天室,用于测试和演示 FB4D 开源库中即将推出的 Firestore 侦听器。 @Bosshoss:是的,这可能是最好的解决方案,但它可能需要比整个演示应用程序更多的代码来控制这个控件。@AmigoJack:要使用richedit,你会是一个解决方案,但我想要一个简短的演示来单击消息用于编辑和删除功能。如果将来我想用发件人的个人资料图像扩展示例,TListView 或 TListBox 会更合适。

标签: delphi firemonkey tlistview


【解决方案1】:

使用ItemAppearance=ListItem 无法解决此问题,因为在内部ResetView 方法中,Item.Objects.TextObject 的所有属性都将写回ItemAppearanceObjects.ItemObjects

因此,对于此应用程序,ItemAppearance 必须设置为 DynamicAppearance。必须在ListView.ItemAppearance.Item 下的结构视图 中手动创建两个TextObjectAppearance 对象,其中Text1 必须设置为TextAlign=LeadingText2 必须设置为TextAlign=Trailing

之后,只有一个文本对象需要被文本填充:

procedure TForm1.FormCreate(Sender: TObject);

  procedure AddItem(No: integer);
  var
    Item: TListViewItem;
  begin
    Item := ListView1.Items.AddItem(No);
    if No mod 2 = 0 then
      Item.Data['Text1'] := 'Text item No ' + No.ToString
    else
      Item.Data['Text2'] := 'Text item No ' + No.ToString
  end;

var
  c: integer;
begin
  for c := 0 to 9 do
    AddItem(c);
end;

这里是对应的FMX文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object ListView1: TListView
    ItemAppearanceClassName = 'TDynamicAppearance'
    ItemEditAppearanceClassName = 'TDynamicAppearance'
    HeaderAppearanceClassName = 'TListHeaderObjects'
    FooterAppearanceClassName = 'TListHeaderObjects'
    Align = Client
    Size.Width = 640.000000000000000000
    Size.Height = 480.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    ItemAppearanceObjects.ItemObjects.ObjectsCollection = <
      item
        AppearanceObjectName = 'Text1'
        AppearanceClassName = 'TTextObjectAppearance'
        Appearance.TextAlign = Leading
      end
      item
        AppearanceObjectName = 'Text2'
        AppearanceClassName = 'TTextObjectAppearance'
        Appearance.TextAlign = Trailing
      end>
    ItemAppearanceObjects.ItemEditObjects.ObjectsCollection = <
      item
        AppearanceObjectName = 'Text1'
        AppearanceClassName = 'TTextObjectAppearance'
      end>
  end
end

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多