FireMonkey TListView:如何在运行时为项目着色?
FireMonkey TListView: How to color items at runtime?
可以为 VCL TListView 使用自定义绘图,它允许您为单个项目着色(here is an example 使用相当狡猾的方法将 TColor
放在 Data
一个项目。)
但是在XE7 FMX版本的TListVew
控件中,没有OnCustomDraw
事件。我尝试了一些选项,但似乎没有任何改变颜色。我该怎么做?
您可以使用 TListViewItem.Objects.DetailObject.TextColor
属性 更改每个列表项文本的颜色。但是没有本机 属性 用于更改每个列表项的背景颜色。
根据TListView Items background color:
How to change the background color of TListView Items?
...
You have to use a style. So add a stylebook en either use one of the
styles that come with Delphi or right click the listview "edit custom
style"to make the style. dubbleclick on the stylebook to open the style
and modify the listviewstyle.
但这并没有解释如何对每个列表项应用不同的样式。
每个项目都有一个 OnPaint 事件。分配一个事件处理程序,如下所示。
procedure TfrmModelArchiver.lst1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
Canvas.Fill.Color := TAlphaColorRec.Red;
Canvas.FillRect(ARect, 0, 0, AllCorners, 0.2);
end;
在此事件处理程序中,不透明度设置为 0.2,因此文本可以显示出来。如果将不透明度设置为 1,则需要编写文本并绘制背景,如下所示。
procedure TfrmModelArchiver.lst1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
Canvas.Fill.Color := TAlphaColorRec.Red;
Canvas.FillRect(ARect, 0, 0, AllCorners, 1);
Canvas.Fill.Color := TAlphaColorRec.Black;
Canvas.FillText(ARect, 'some text', False, 1, [],
TTextAlign.taLeading);
end;
Here 是解决方案。
此现代 ListView 组件可用于 Delphi 10 Berlin with FireMonkey Android、IOS、OSX 和 Windows。一个不错的功能是它具有水平和垂直模式。
SetColorItemSelected、SetColorItemFill、SetColorBackground、SetColorItemSeparator、SetColorText、SetColorTextSelected、SetColorTextDetail、SetColorHeader、SetColorTextHeader 和许多其他属性。
可以为 VCL TListView 使用自定义绘图,它允许您为单个项目着色(here is an example 使用相当狡猾的方法将 TColor
放在 Data
一个项目。)
但是在XE7 FMX版本的TListVew
控件中,没有OnCustomDraw
事件。我尝试了一些选项,但似乎没有任何改变颜色。我该怎么做?
您可以使用 TListViewItem.Objects.DetailObject.TextColor
属性 更改每个列表项文本的颜色。但是没有本机 属性 用于更改每个列表项的背景颜色。
根据TListView Items background color:
How to change the background color of TListView Items?
...
You have to use a style. So add a stylebook en either use one of the styles that come with Delphi or right click the listview "edit custom style"to make the style. dubbleclick on the stylebook to open the style and modify the listviewstyle.
但这并没有解释如何对每个列表项应用不同的样式。
每个项目都有一个 OnPaint 事件。分配一个事件处理程序,如下所示。
procedure TfrmModelArchiver.lst1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
Canvas.Fill.Color := TAlphaColorRec.Red;
Canvas.FillRect(ARect, 0, 0, AllCorners, 0.2);
end;
在此事件处理程序中,不透明度设置为 0.2,因此文本可以显示出来。如果将不透明度设置为 1,则需要编写文本并绘制背景,如下所示。
procedure TfrmModelArchiver.lst1Paint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
begin
Canvas.Fill.Color := TAlphaColorRec.Red;
Canvas.FillRect(ARect, 0, 0, AllCorners, 1);
Canvas.Fill.Color := TAlphaColorRec.Black;
Canvas.FillText(ARect, 'some text', False, 1, [],
TTextAlign.taLeading);
end;
Here 是解决方案。
此现代 ListView 组件可用于 Delphi 10 Berlin with FireMonkey Android、IOS、OSX 和 Windows。一个不错的功能是它具有水平和垂直模式。 SetColorItemSelected、SetColorItemFill、SetColorBackground、SetColorItemSeparator、SetColorText、SetColorTextSelected、SetColorTextDetail、SetColorHeader、SetColorTextHeader 和许多其他属性。