FMX: Autosize TFlowLayout

    • Offizieller Beitrag

    Instead of translating VCL a form to firemonkey, I find it more useful to recreate it using the best technique firemonkey has to offer.

    Firemonkey layout controls are quite useful to create a form which realigns its controls when the size of the forms was changed. This is needed in case you do not know if the form would be used in landscape or portrait mode. But how to make the layout size itself according to its contents? Currently there does not seem to be an autosize property.

    Fortunately there is the Resized event. If you assign an event handler which does the autosizing, it will work quite nicely. You can even place your

    TFlowlayout inside of a TScrollbox so the form will display scrollbars as soon as required.

    The event handler can be the same for all the layout controls and just needs one line of code:

    Code
    procedure TForm1.AllFlowLayoutResized(Sender: TObject);
    begin
       WPResizeTControlToContents( Sender as TControl );
    end;

    Of course the utility function WPResizeTControlToContents must be defined somewhere else in the project, preferably in a unit all the other units include.

    The following code automatically checks the align property to disable horizontal/vertical sizing.

    It is worth to note that only the first level of the children of the control are checked for size.

    The function TControl.ChildrenRect btw did not work as expected.