![](/img/trans.png)
[英]Firemonkey TListBox changing background color of item rows at runtime
[英]Firemonkey TListBox changing background color at runtime
除了使用 styles,我有辦法在運行時更改 TListBox 的背景顏色嗎? 我可以使用 OnPaint 事件嗎?
因為TListbox
沒有更改背景顏色的屬性,所以我只能想到以下內容,它基於組合兩個組件,其中一個( TListBox
)使用內置樣式。 但是請注意,這不依賴於TStyleBook
或 Delphi Firemonkey 提供的任何樣式文件。
放置一個TRectangle
作為TListBox
的背景。 將其Fill
- Color
屬性設置為您喜歡的顏色。 (我在示例中使用了“Cornsilk”)。
將TListBox
作為矩形的子元素放置在矩形上。 在“對象檢查器”中找到StyleLookup
屬性並將其值更改為transparentlistboxstyle
。 這使得列表框透明,矩形及其填充顏色閃耀。
如果使TListBox
每邊都比矩形小一個像素,則可以使用Sides
屬性在列表框周圍提供一個細框。 或者您可以選擇使它們大小相同而不顯示任何框架。
我的測試結果如下所示:
.fmx
文件中的TRectangle
和TListbox
屬性:
object Rectangle1: TRectangle
Anchors = [akLeft, akTop, akBottom]
Fill.Color = claCornsilk
Position.X = 7.000000000000000000
Position.Y = 40.000000000000000000
Size.Width = 361.000000000000000000
Size.Height = 219.000000000000000000
Size.PlatformDefault = False
object ListBox1: TListBox
Anchors = [akLeft, akTop, akRight, akBottom]
Position.X = 1.000000000000000000
Position.Y = 1.000000000000000000
Size.Width = 359.000000000000000000
Size.Height = 217.000000000000000000
Size.PlatformDefault = False
StyleLookup = 'transparentlistboxstyle'
TabOrder = 0
ParentShowHint = False
ShowHint = False
DisableFocusEffect = True
ItemHeight = 48.000000000000000000
DefaultItemStyles.ItemStyle = 'listboxitemrightdetail'
DefaultItemStyles.GroupHeaderStyle = ''
DefaultItemStyles.GroupFooterStyle = ''
Viewport.Width = 359.000000000000000000
Viewport.Height = 217.000000000000000000
end
end
要更改ListBox1
的顏色,您實際上更改了TRectangle
的顏色:
procedure TForm5.ColorListBox1ItemClick(const Sender: TCustomListBox;
const Item: TListBoxItem);
begin
Rectangle1.Fill.Color := TColorListBox(Sender).Color;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.