Firemonkey:如何正确对齐标签?

时间:2017-05-20 10:37:05

标签: delphi firemonkey

如果我这样做

RectangleCont.beginUpdate;

myText1 := TText.Create(RectangleCont);
myText1.parent := RectangleCont;
myText1.text := 'a long text'; 
myText1.Position.x := RectangleCont.width - myText1.width; // 150px for exemple
myText1.align := TalignLayout.right;

myText2 := TText.Create(RectangleCont);
myText2.parent := RectangleCont;
myText2.text := 'OK';
myText2.Position.x := RectangleCont.width - myText1.width - myText2.width; // 50px for exemple
myText2.align := TalignLayout.right;

myText3 := TText.Create(RectangleCont);
myText3.parent := RectangleCont;
myText3.text := 'OK';
myText3.Position.x := RectangleCont.width - myText1.width - myText2.width - myText3.width; // 0px for exemple
myText3.align := TalignLayout.right;

RectangleCont.EndUpdate;

我将<myText1><myText3><myText2>代替<myText3><myText2><myText1>

注意:如果我在myText1.text中添加较短的文字,那么它就可以正常使用。

有解决方法吗?

2 个答案:

答案 0 :(得分:1)

Delphi Align属性适用于大多数情况,但是当您有更多罕见的需求时,编写自己的resize事件并不困难。只需跳过在代码中设置AlignPosition属性,而不是使用此代码(它未经测试,但我认为您可以理解,以防我忘记了某些内容):

...
RectangleCont.OnResize := DoResizeRectangle;
...


procedure TMainForm.DoResizeRectangle(aSender: TObject);
var lParent: TControl;
begin
  lParent := TControl(aSender);
  myText1.Height := lParent.Height;
  myText2.Height := lParent.Height;
  myText3.Height := lParent.Height;
  myText1.Position.Point := PointF(lParent.Width-myText1.Width, 0);
  myText2.Position.Point := PointF(myText1.Position.X-myText2.Width, 0);
  myText3.Position.Point := PointF(myText2.Position.X-myText3.Width, 0);
end;

答案 1 :(得分:-1)

myText1.align := TalignLayout.left;
myText2.align := TalignLayout.right;
myText1.align := TalignLayout.right;