我正在尝试在运行时将一组组件添加到
GridpanelLayout
。在本例中为 4 组,每组都是 Tlable
Tbutton
Tedit
Tbutton
。每个组件应位于 1 行,并且每个组件位于其自己的列中。
当我运行下面的代码时,它将添加 2 组组件,并且第一组的
Tbutton
、 TEdit
和 TButton
在调整大小时不会与列对齐,但所有其他组件都会这样做。下面是我的代码。另请注意,我有一个 TLayout
,并且 TGridPanelLayout
位于与客户端对齐的 TLayout
内部。
procedure TForm5.Button1Click(Sender: TObject);
var
I,K : integer;
Namelabel : TLabel;
MinusButton, PlusButton : TButton;
EditCount : TEdit;
begin
I:= 0;
K:= 0;
for I := 0 to 2 do
Begin
namelabel := Tlabel.Create(self);
MinusButton := TButton.Create(self);
EditCount := TEdit.Create(self);
PlusButton := TButton.Create(self);
Namelabel.name := 'LabelField_'+i.ToString;
MinusButton.Name := 'MinusField_'+i.ToString;
PlusButton.Name := 'PlusField_'+I.ToString;
EditCount.Name := 'EditField_'+i.ToString;
MinusButton.Text := '-';
PlusButton.Text := '+';
MinusButton.Width := 20;
PlusButton.Width := 20;
namelabel.Parent := GridPanelLayout1;
MinusButton.Parent := GridPanelLayout1;
EditCount.Parent := GridPanelLayout1;
PlusButton.Parent := GridPanelLayout1;
GridPanelLayout1.BeginUpdate;
GridPanelLayout1.RowCollection.BeginUpdate;
if i <> 0 then
begin
GridpanelLayout1.RowCollection.Add;
GridPanelLayout1.RowCollection[i].SizeStyle := TGridpanelLayout.TSizeStyle.Auto;
end;
GridPanelLayout1.RowCollection.EndUpdate;
With GridpanelLayout1 do
begin
ControlCollection.BeginUpdate;
ControlCollection.Add;
controlcollection[i+k].Column := 0;
ControlCollection[i+k].Row := i;
controlcollection[i+k].ColumnSpan := 1;
ControlCollection[i+k].RowSpan := 1;
ControlCollection[i+k].Control := (namelabel);
K:= K+1;
ControlCollection.Add;
controlcollection[i+k].Column := 1;
ControlCollection[i+k].Row := i;
controlcollection[i+k].ColumnSpan := 1;
ControlCollection[i+k].RowSpan := 1;
ControlCollection[i+k].Control := (MinusButton);
K:= K+1;
ControlCollection.Add;
controlcollection[i+k].Column := 2;
ControlCollection[i+k].Row := i;
controlcollection[i+k].ColumnSpan := 1;
ControlCollection[i+k].RowSpan := 1;
ControlCollection[i+k].Control := (EditCount);
K:= K+1;
ControlCollection.Add;
controlcollection[i+k].Column := 3;
ControlCollection[i+k].Row := i;
controlcollection[i+k].ColumnSpan := 1;
ControlCollection[i+k].RowSpan := 1;
ControlCollection[i+k].Control := (PlusButton);
K:= K+1;
controlCollection.EndUpdate;
showmessage(inttostr(RowCollection.Count))
end;
GridPanelLayout1.EndUpdate;
End;
end;
更新***** 添加之前和之后的图像。 之前 - 一切都对齐。
FMX File
object Form5: TForm5
Left = 0
Top = 0
Caption = 'Form5'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
DesignerMasterStyle = 0
object Layout1: TLayout
Align = Client
Size.Width = 640.000000000000000000
Size.Height = 430.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
object GridPanelLayout1: TGridPanelLayout
Align = Client
Size.Width = 640.000000000000000000
Size.Height = 430.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
ColumnCollection = <
item
Value = 25.366992464791410000
end
item
Value = 24.910521746363720000
end
item
Value = 24.580453329747010000
end
item
Value = 25.142032459097870000
end>
ControlCollection = <>
RowCollection = <
item
SizeStyle = Auto
Value = 100.000000000000000000
end
item
SizeStyle = Auto
Value = 100.000000000000000000
end>
end
end
object Layout2: TLayout
Align = Bottom
Position.Y = 430.000000000000000000
Size.Width = 640.000000000000000000
Size.Height = 50.000000000000000000
Size.PlatformDefault = False
TabOrder = 1
object Button1: TButton
Position.X = 440.000000000000000000
Position.Y = 16.000000000000000000
TabOrder = 0
Text = 'Button1'
OnClick = Button1Click
end
object Label1: TLabel
Position.X = 32.000000000000000000
Position.Y = 16.000000000000000000
Text = 'Label1'
TabOrder = 1
end
object Edit1: TEdit
Touch.InteractiveGestures = [LongTap, DoubleTap]
TabOrder = 2
Position.X = 224.000000000000000000
Position.Y = 16.000000000000000000
end
end
end
当您分配这些控件的父级时,您在循环开始时创建的控件将添加到
GridPanelLayout1
:
Namelabel.Parent := GridPanelLayout1;
MinusButton.Parent := GridPanelLayout1;
EditCount.Parent := GridPanelLayout1;
PlusButton.Parent := GridPanelLayout1;
将控件添加到
ControlCollection
的代码弄乱了内部簿记。
因此,在代码之后:
GridPanelLayout1.BeginUpdate;
GridPanelLayout1.RowCollection.BeginUpdate;
GridPanelLayout1.RowCollection.Add;
GridPanelLayout1.RowCollection[I].SizeStyle :=
TGridPanelLayout.TSizeStyle.Auto;
删除代码...
With GridPanelLayout1 do
begin
ControlCollection.BeginUpdate;
...
end;
直到
GridPanelLayout1.RowCollection.EndUpdate;
GridPanelLayout1.EndUpdate;
...并且控件已正确设置父级并根据您的需要插入到行/列中。
我还注意到一个问题,那就是收缩网格面板时尺寸会减小。当您再次加宽网格时,控件不会返回到原始宽度。现在我必须让你考虑你想做什么。