Delphi XE8 Firemonkey TListView - 如何以编程方式设置背景颜色

时间:2015-08-26 14:28:05

标签: delphi firemonkey delphi-xe8 tlistview

TListView与样式相关联。此样式包含名为background的TColorObject。如果在样式设计器中设置TColorObject.Color(红色),Treeview将显示此颜色。 如果在TListView的ApplyStyleLookup事件中以编程方式设置Color,则背景颜色将保留在样式中设置的颜色(红色)!!!!

procedure TTest.TreeViewlistApplyStyleLookup(Sender: TObject);
var
 co: TColorObject;
begin
co := nil;
if Sender is TListView then  co := TListView(Sender).FindStyleResource('background') as TColorObject;
if co <> nil then co.Color := TAlphaColors.Black;    
//co is not nil 
//TColorObject background is found and black is set, but it remains on red
end;

2 个答案:

答案 0 :(得分:2)

您可以添加帮助程序以在TCustomListView中设置私有变量 虽然由于某种原因它只在你添加了一些项目

后才有效
unit ListViewHelper;

interface
uses  FMX.ListView, System.UITypes ;
type
  TListViewHelper = class helper for TCustomListView
    procedure SetItemStyleFillColour(Colour : TAlphaColor);
    procedure SetBackgroundStyleColor(Colour : TAlphaColor);
  end;

implementation

{ TListViewHelper }

procedure TListViewHelper.SetBackgroundStyleColor(Colour: TAlphaColor);
begin
  TCustomListView(self).FBackgroundStyleColor := Colour;
end;

procedure TListViewHelper.SetItemStyleFillColour(Colour: TAlphaColor);
begin
  TCustomListView(self).FItemStyleFillColor := Colour;
end;

end.

然后在任何想要更改背景颜色的地方使用该单位并调用SetItemStyleFillColour

  

lv1.SetItemStyleFillColour(TAlphaColor($ FF4A494A));
  lv1.SetBackgroundStyleColor(TAlphaColorRec.Blue);

e.g。

unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.ListView.Types, FMX.ListView;

type

  TForm1 = class(TForm)
    lv1: TListView;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses ListViewHelper;

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
  lv1.Items.Add.Text := FormatDateTime('dd-mm-yyyy HH:NN:SS ZZZ', Now());
  lv1.Items.Add.Text := FormatDateTime('dd-mm-yyyy HH:NN:SS ZZZ', Now());
  lv1.SetItemStyleFillColour(TAlphaColor($FF4A494A));
  lv1.SetBackgroundStyleColor( TAlphaColorRec.Blue);
end;

end.

答案 1 :(得分:0)

您可以将TListView放在TRectangle中,并将listview的Transparent属性设置为True。