在运行时向GridPanelLayout添加组件

3
我正在尝试在运行时向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

不清楚出了什么问题。也许提供一个显示问题的图片会有助于理解。你可以编辑问题并添加图片。 - undefined
@GermánEstévez-Neftalí- 添加了图片以帮助展示。 - undefined
谢谢,格伦!我现在能看到问题了,但还没有找到解决办法。稍后我会回来的。 - undefined
谢谢@TomBrunberg,我也在努力尝试,但我觉得奇怪的是只有3个组件出现问题,但它们的创建方式与其他组件相同。 - undefined
1个回答

2
在循环开始时创建的控件将在将这些控件的父级指定为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;

...并且控件已经正确地作为您所希望的方式进行了父子关联和插入到行/列中。
我注意到还有一个问题,那就是当您收缩网格面板时,尺寸会减小。当您再次扩大网格时,控件并不会恢复到原始宽度。由于这是另一个问题,我现在只能让您自行决定如何处理。
关于调整(收缩)窗体宽度时出现的问题的更新。Delphi 11 FMX TForm现在有一个Constraints属性,就像VCL Tform一样“永远”存在(或者至少我记得很久了)。但在我看来,这只是一个权宜之计。

谢谢,到目前为止,我还没有看到你所说的错误,在调整大小之后。可能是因为我的原始代码与这里的代码略有不同。但我也会查看这个问题。谢谢。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接