Question

How do I center an object in the form.

in the VCL I usually use

lblID.Left := (MainForm.Width div 2) - (lblID.Width div 2);

but the above code is not working in FMX because the Width of any object is single not integer.

I've tried the following but stil it does not center it perfectly

lblID.Position.X := (MainForm.Width div 2) - trunc(lblID.Width)  div 2;

Whats wrong ?

Was it helpful?

Solution

The TPosition property of FMX controls is stored in floating point, not integer, format. There is no need to use integer division.

 lblID.Position.X := 0.5*(MainForm.Width - lblID.Width);

You should probably also review :

For example :

lblID.Align := alHorzCenter;

may give you want you want. Adusting the Padding property will allow for vertical placement of the control while alHorzCenter will keep the control horizontally centered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top