我只想在按下按钮后改变它的颜色。
我是不是必须使用“样式”来完成这项工作,或者...?
发布于 2011-11-09 02:21:53
您可以更改button.StyleLookup属性以更改样式(颜色)。
您需要将新样式添加到Stylebook中。
的名称时,将background:TRectangle
发布于 2011-11-09 04:02:08
使用样式的
创建不同样式并切换到该新样式的另一种方法是为按钮创建自定义样式,然后在运行时更改该样式中的颜色。
您刚刚为该按钮创建了一个自定义样式。因此,当您在运行时编辑它时,它只会影响该按钮。
现在,在OnClick事件中输入以下内容以在运行时更改颜色:
var
r: TRectangle;
begin
// Find the background TRectangle style element for the button
r := (Button1.FindStyleResource('background') as TRectangle);
if Assigned(r) then
begin
r.Fill.Color := claBlue;
end;
end;
注意:如果您还没有FMX.Objects,请将它添加到uses子句中。这就是TRectangle所在的地方。
但请稍等...
您会注意到,当鼠标离开或进入按钮时,按钮的颜色将更改回默认颜色。这要归功于动画。如果在自定义样式的样式编辑器中为这两个TColorAnimation样式元素设置了stylename属性,则还可以在这些元素上设置颜色。在我的示例中,我将TColorAnimations命名为coloranimation1和coloranimation2。
以下是修改后的代码:
var
r: TRectangle;
ca: TColorAnimation;
begin
// Find the background TRectangle style element for the button
r := (Button1.FindStyleResource('background') as TRectangle);
if Assigned(r) then
begin
r.Fill.Color := claBlue;
end;
ca := (Button1.FindStyleResource('coloranimation1') as TColorAnimation);
if Assigned(ca) then
begin
ca.StartValue := claBlue;
end;
ca := (Button1.FindStyleResource('coloranimation2') as TColorAnimation);
if Assigned(ca) then
begin
ca.StopValue := claBlue;
end;
注意:将FMX.Ani添加到TColorAnimation的uses子句中。
https://stackoverflow.com/questions/8053545
复制相似问题