0

これが Firemonkey のサンプル コードです。

var
   f: integer; 
   Label1: TLabel;
   MyStringArray: TArray<String>;
   Panel1: TPanel;
   Layout1: TLayout;
begin
   Layout1.Align := TAlignLayout.Client;
   MyStringArray := ['aa','bb','cc','dd','ee','ff'];
   f:= 10;
   Layout1.BeginUpdate;
   for i := 0 to length(MyStringArray) - 1 do
   begin
        Label1 := TLabel.Create(Self);
        Label1.Name := 'Label' + i.ToString;
        Label1.Text := 'Label_' + MyStringArray[i];
        Label1.Position.Y := f;
        Label1.Align := TAlignLayout.Top;
        Label1.Parent := Layout1;
        inc(f, 15);
   end;
   Layout1.EndUpdate;
end; 

MyStringArray は常に同じ数の要素を持つ動的配列ではないため、ラベルの数に応じて TLayout (Layout1) の内容で TPanel (Panel1) のサイズを変更します。

Panel1.Height := Layout1.ChildrenRect.Height

これは、Layout1 でラベルの数が増えると問題なく機能しますが、ラベルの数が少なく、Layout1.ChildrenRect.Height効果がなく、縮小もしない場合、Layout1 の高さは常に高い値を維持します。

それを行う方法の解決策または他の代替手段はありますか?, よろしくお願いします.

4

2 に答える 2

1

バグレポートとして以下を提出しました。それまでの間、以下のコードから始めて、境界を自分で計算することをお勧めします。

FMX TControl.ChildrenRect のドキュメントには次のように記載されています。

"現在のコントロールの子が占める四角形の領域を指定します。ChildrenRect は、コントロールの子が占める四角形の間で結合操作を実行することによって取得される四角形です。" - http://docwiki.embarcadero.com/Libraries/XE7/en/FMX.Controls.TControl.ChildrenRect

ただし、コードには実際には計算に独自の境界が含まれています。

function TControl.GetChildrenRect: TRectF;
var
  I: Integer;
  Control: TControl;
begin
  Result := AbsoluteRect;  <---*****This line
  { children }
  if not (ClipChildren or SmallSizeControl) and (FControls <> nil) then
    for I := GetFirstVisibleObjectIndex to GetLastVisibleObjectIndex - 1 do
    begin
      Control := FControls[I];
      if Control.Visible then
        Result := UnionRect(Result, Control.GetChildrenRect);
    end
end;

これが意図された動作である場合は、ドキュメントを更新する必要があります。そうでない場合は、実装のバグです。

于 2015-02-16T16:42:20.630 に答える
1

このコードがその助けになることを願っています。さらに編集して申し訳ありません。これが最後ですが、もう一度試してみたところ、コードが完全に正しくありませんでした。

type
  TControlHelper = class helper for TControl
  public
    function ChildrenWidth: Single; 
    function ChildrenHeight: Single; 
  end;

function TControlHelper.ChildrenWidth: Single;
var
  VIndex: Integer;
  VControl: TControl;
  VRect: TRectF;
begin
  VRect := TRectF.Empty;
  if not ( ClipChildren or SmallSizeControl ) and ( Controls <> nil ) then
  begin
    for VIndex := GetFirstVisibleObjectIndex to GetLastVisibleObjectIndex - 1 do
    begin
      VControl := Controls[ VIndex ];
      if VControl.Visible then
        VRect := UnionRect( VRect, VControl.Margins.MarginRect( TRectF.Create( VControl.Position.Point, VControl.Width, VControl.Height ) ) );
    end;
    Result := VRect.Width + Padding.Right;
  end;
end;

function TControlHelper.ChildrenHeight: Single;
var
  VIndex: Integer;
  VControl: TControl;
  VRect: TRectF;
begin
  VRect := TRectF.Empty;
  if not ( ClipChildren or SmallSizeControl ) and ( Controls <> nil ) then
  begin
    for VIndex := GetFirstVisibleObjectIndex to GetLastVisibleObjectIndex - 1 do
    begin
      VControl := Controls[ VIndex ];
      if VControl.Visible then
        VRect := UnionRect( VRect, VControl.Margins.MarginRect( TRectF.Create( VControl.Position.Point, VControl.Width, VControl.Height ) ) );
    end;
    Result := VRect.Height + Padding.Bottom;
  end;
end;
于 2015-03-26T23:57:40.670 に答える