【问题标题】:Delphi XE8 Firemonkey TListView - How to set a background color programmaticallyDelphi XE8 Firemonkey TListView - 如何以编程方式设置背景颜色
【发布时间】:2015-11-20 15:46:34
【问题描述】:

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

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;

【问题讨论】:

    标签: delphi firemonkey delphi-xe8 tlistview


    【解决方案1】:
    use System.Rtti; 
    
    TRttiContext.Create.GetType(TListView).GetField('FBackgroundStyleColor').SetValue(ListView2, TAlphaColorRec.Orange);
    
    TRttiContext.Create.GetType(TListView).GetField('FItemStyleFillColor').SetValue(ListView2, TAlphaColorRec.Azure);
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案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);

    例如

    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.
    

    【讨论】:

    • 这不再适用于 Delphi Seattle+,因为这两个变量现在都是私有的。
    【解决方案3】:

    你可以把TListView放在TRectangle里面,把listview的Transparent属性设置为True。

    【讨论】:

    • 如果我设置透明度,也没有 ListViewItem 可见,但我认为它可以工作,如果我将样式中的背景颜色设置为透明颜色,但如果 TListView 有可以应用只能在设计器中更改的样式..
    猜你喜欢
    • 2021-10-18
    • 2023-03-26
    • 2014-06-24
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多