【问题标题】:Dynamic Create and Align Labels in Firemonkey [duplicate]在 Firemonkey 中动态创建和对齐标签 [重复]
【发布时间】:2013-06-25 16:22:26
【问题描述】:

我制作了这个程序,将动态 TLabel 对象创建到我的 firemonkey 表单中的一个 TLayout 组件中。

procedure TForm1.printinLayout14(const str: String);
var
  P:TLabel;
begin
  P:=TLabel.Create(Self);
  P.parent:=Layout14;
  p.Align:=TAlignLayout.alTop;
  p.Text:=str;
  p.AutoSize:=true;
  Application.ProcessMessages;
end;

在我的代码中,我用下一种方式调用这个过程

printinLayout14('l1');
printinLayout14('l2');
printinLayout14('l3');
printinLayout14('l4');

预期的结果是:

---
l1
---
l2
---
l3
---
l4
---

但最终的结果是

---
l1
---
l4
---
l3
---
l2
---

我该如何解决它?我正在使用 Delphi Xe3

【问题讨论】:

    标签: delphi dynamic components firemonkey


    【解决方案1】:

    我终于解决了。

    解决方案: 根据@NGLN 在 How to dynamically create controls aligned to the top but after other aligned controls?

    当已经有另一个控件与顶部对齐时,则有两个控件Top = 0,即将插入的控件获胜。

    因此,为了避免这种情况,我在 ALign:=alTop 之前手动为 TLayoutComponent 中以这种方式创建的任何新子项分配了一个不同的 Position.Y 属性:

    p.Position.Y:=p.Widht*(Layout14.ChildrenCount-1);
    p.Align:=TAlignLayout.alTop;
    

    【讨论】:

    • 您的解释解决方案只是幸运的一击。您使用宽度而不是高度,并希望每个孩子都有相同的高度
    • 你可以在 Position.Y 中设置一个非常高的数字(例如 2000)并获得相同的效果。
    • @MikeSutton 最好的价值是System.Math.MaxSingle :o)
    猜你喜欢
    • 2012-12-14
    • 1970-01-01
    • 2023-03-14
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    相关资源
    最近更新 更多