Delphi / FMX:如何在所有先前添加的顶部对齐组件下添加动态创建的顶部对齐组件,而不是从顶部添加第二个?

问题描述 投票:1回答:2

我正在使用Delphi和FMX为Android开发应用程序。在按钮的onclick过程中,我动态创建了一个TPanel(其中包含一些组件),然后将其添加到TVertScrollBox中。我希望TPanels彼此堆叠,所以我将Align属性设置为Top。

procedure TMainForm.AddGroupButtonClick(Sender: TObject);
var Group : TPanel;
begin
   Group := TPanel.Create(Self);
   Group.Parent := Groups;         // where Groups is a TVertScrollBox on the form
   Group.Align := TAlignLayout.Top;

   //Then I create some other components and set their Parent to Group
end;

用户可能希望将新的TPanel添加到所有其他TPanels下。但是,除非以前没有添加TPanels,否则将每个新的TPanel直接添加到最上面的一个,即从顶部开始的第二个。

这是为什么,以及如何在所有先前添加的TPanel下添加新的TPanel?

我在这里看到了类似的问题,但是他们使用的是VCL,显然您可以更改顶级属性。不过,使用FMX组件时似乎没有一个。

delphi firemonkey
2个回答
0
投票

[创建新的Firemonkey面板时,其Position属性默认为X=0, Y=0

[设置Align := TAlignLayout.Top;时,它将与先前放置的组件进行比较,发现在Y = 0处已经有一个面板,并在现有面板下方挤压新面板。

将新面板放置在所有其他面板集下方

...
Group.Position.Y := 1000; // any sufficiently large value (bigger than the last panel's Y)
Group.Align := TAlignLayout.Top;

0
投票

这是因为当您动态创建TPanel时,它具有(0,0)位置,因此它比所有其他先前创建的面板高。您可以将作弊方法用于要在其他面板下面创建的新创建的面板。这是两个简单的解决方案。

方法1:

   Group := TPanel.Create(Self);
   Group.Parent := Groups;         // where Groups is a TVertScrollBox on the form
   Group.Align := TAlignLayout.bottom; // :D it firstly created it at bottom and then move it to top
   Group.Align := TAlignLayout.Top;

方法2:

   Group := TPanel.Create(Self);
   Group.Parent := Groups;         // where Groups is a TVertScrollBox on the form
   Group.Position.Y:=Groups.Height+1;
   Group.Align := TAlignLayout.Top;

我希望它可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.