Text Alignment of ListViewItem

  

In Firemonkey of Delphi 10.4. I want to build a list view where the listview item text appears either left- or right-aligned depending on the list item content.

I do this as follows:
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;

However, this does not work on all platforms. But as soon as the ListView changes its size, all ListItems appear flush on the left. Is there a better way to do this?
As a workaround, I have done the following:
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;

Comments are closed.