我正在使用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组件时似乎没有一个。
[创建新的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;
这是因为当您动态创建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;
我希望它可以解决您的问题。