7

我只想在按下按钮后更改按钮的颜色。

我必须使用“样式”来执行此操作还是....?

4

2 回答 2

8

您可以更改 button.StyleLookup 属性以更改样式(颜色)。

您需要向样式簿添加新样式。

  1. 从按钮的鼠标右键菜单中选择“编辑自定义样式...”。
  2. 更改背景下 TRectangle 项的 Fill.Color 属性:TRectangle
  3. 应用并关闭样书
  4. 清除 button.stylelookup
  5. 当您没有更改其 Button1Style1 的名称时,将 buttonclick 中的 button.stylelookup 更改为新的创建样式
于 2011-11-08T18:21:52.887 回答
2

使用样式

创建不同样式并切换到该新样式的替代方法是为按钮创建自定义样式并在运行时更改该样式的颜色。

  1. 右键单击该按钮并从主菜单中选择“编辑自定义样式...”。
  2. 在样式编辑器中单击应用并关闭。

您刚刚为按钮创建了自定义样式。因此,当您在运行时对其进行编辑时,它只会影响该按钮。

现在,在 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 子句中。

于 2011-11-08T20:02:07.687 回答