文本水平对齐在运行时创建的标签上不起作用
在 Android 应用程序中,我尝试在运行时将标签创建为矩形,并且除了文本的水平对齐之外,所有属性都工作正常。我的代码有问题吗?
procedure TForm7.Button1Click(Sender: TObject);
var
lb : TLabel;
begin
lb := TLabel.Create(Rectangle1);
lb.Parent := Rectangle1;
lb.Align := TAlignLayout.Center;
lb.TextSettings.HorzAlign := TTextAlign.Leading;
lb.Width := 300;
lb.TextSettings.Font.Size := 12;
lb.StyledSettings:=[TStyledSetting.Family,TStyledSetting.Style,
TStyledSetting.FontColor,TStyledSetting.Size,TStyledSetting.Other];
lb.Margins.Bottom := 100;
lb.Text := 'Programming Language is Delphi 10.4 31/3/2022';
end;
In an Android app, I'm trying to create a Label at runtime into a rectangle, and all of the properties works fine except for the Horizontal Align of the text. Is something wrong with my code?
procedure TForm7.Button1Click(Sender: TObject);
var
lb : TLabel;
begin
lb := TLabel.Create(Rectangle1);
lb.Parent := Rectangle1;
lb.Align := TAlignLayout.Center;
lb.TextSettings.HorzAlign := TTextAlign.Leading;
lb.Width := 300;
lb.TextSettings.Font.Size := 12;
lb.StyledSettings:=[TStyledSetting.Family,TStyledSetting.Style,
TStyledSetting.FontColor,TStyledSetting.Size,TStyledSetting.Other];
lb.Margins.Bottom := 100;
lb.Text := 'Programming Language is Delphi 10.4 31/3/2022';
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要阅读 FMX.Graphics.ITextSettings。您会发现
TStyledSettings
和TTextSettings
之间存在重要关系。在您的问题中,您担心水平对齐不遵循您的设置:
这是因为您通过在
lb.StyledSettings
中包含TStyledSetting.Other
来否决它。从
lb.StyledSettings
中删除TStyledSetting.Other
,您将看到HorzAlign
、VertAlign
、Trimming
和WordWrap
将遵循您自己的设置。You need to read up on FMX.Graphics.ITextSettings. You will find that there is an important relation between
TStyledSettings
andTTextSettings
.In your question you are concerned with horizontal alignment not following your setting:
That is because you have overruled it by including
TStyledSetting.Other
inlb.StyledSettings
.Remove that
TStyledSetting.Other
fromlb.StyledSettings
and you will see thatHorzAlign
,VertAlign
,Trimming
andWordWrap
will follow your own settings.我希望这可以帮助你。
TLabel
的属性Alignment
需要配置为taCenter
。但仅仅这样还不够。除此之外,您还需要将属性AutoSize
配置为false
。那么,一切都会好起来的。属性
Align
不是对齐标签内的文本,而是对齐表单内的标签组件。Alignment
是对齐标签内文本的正确属性。是的,我知道,他们可以更清楚地提出这些属性名称。I hope this could help you. The property
Alignment
ofTLabel
need to be configured totaCenter
. But just it isn't enough. Beyond that, you need to configure propertyAutoSize
tofalse
. Then, everything will be OK.The property
Align
isn't to align text inside label, but label component inside form. TheAlignment
is the correct property to align text inside label. Yes, I know, they could be clearer in coming up with these property names.