问题
In my program, i made a function which reads a xml of a blog and put the Titles in a TListBox
. But i need to change some properties in TListBoxItem
, like font, height and color, but it doesn't change.
How to set it in runtime ?
repeat
Title := ANode.ChildNodes['title'].Text;
Link := ANode.ChildNodes['link'].Text;
Desc := ANode.ChildNodes['description'].Text;
DataPub := ANode.ChildNodes['pubDate'].Text;
SetLength(Vet_News, Pos + 1);
Vet_Nesw[Pos] := '<h2>'+Title+'</h2>'+Desc;
Itemx := TListBoxItem.Create(self);
Itemx.Text := Title;
Itemx.ItemData.Detail := DataPub;
Itemx.ItemData.accessory := TListBoxItemData.TAccessory.aMore;
Itemx.TextSettings.WordWrap := true;
Itemx.TextSettings.FontColor := TAlphaColorRec.Darkorange;
Itemx.Height := 65;
Itemx.FontColor := TAlphaColorRec.Darkorange; // i tried two ways to change the color
lbNews.AddObject(Itemx); // lbnews is a Tlistbox
Inc(Pos);
ANode := ANode.NextSibling;
until ANode = nil;
回答1:
[Tested with Delphi-XE7]
At runtime, Listboxitems already have a calculated style stored in : aListboxItem.StyledSettings
.
To change a setting at runtime, you first have to remove it from the list of styled settings.
For example, if you want to change the FontColor
, first remove the styled fontcolor:
aListboxItem.StyledSettings := aListboxItem.StyledSettings - [TStyledSetting.FontColor];
Then apply another one (let's say Green):
aListboxItem.FontColor := TAlphaColors.Green;
The TStyledSetting
constants and corresponding TTextSettings
properties are listed here in Delphi's doc.
Examples for changing styles at runtime can be found here and there.
Nota Bene: theListBox.Items[i]
gives access to item content, not the item itself.
To grab a ListboxItem as a control, and then act on its properties, you can use:
aListboxItem := theListBox.ListItems[i];
or
aListboxItem := theListBox.ItemByIndex(i);
Both giving exactly the same result, I cannot say if one is better.
来源:https://stackoverflow.com/questions/27062707/how-to-change-properties-of-a-tlistboxitem-in-a-android-app-in-runtime