根据布局尺寸调整控件大小?

时间:2016-12-12 15:24:35

标签: smart-mobile-studio

好的,我需要根据布局尺寸调整控件的大小。我还需要在布局尺寸发生变化时调整大小(例如将设备从轮廓转向横向)

我的假设是你只是检查设备的尺寸并相应调整

e.g。

if ClientHeight > ClientWidth then
    fHeader.Height:= ClientHeight Div 6
   else
    fHeader.Height:= ClientHeight Div 8;

但是,放置这个的最佳位置在哪里?

我在调整大小之前和调整大小后调整了Resize方法。它似乎不起作用!

嗯,它适用于表单的初始激活,但是当表单调整大小时,它不会。在IDE中,我甚至必须点击RELOAD按钮才能使其工作。

下面是表单上有两个组件的示例。 TW3HeaderControl与表单顶部对齐,TW3ListBox与客户端对齐。 我想调整TW3HeaderControl的高度,不论它是在Profile还是Landscape中

e.g。

unit Form1;


interface


uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Layout,
  SmartCL.Controls;


type
  TForm1 = class(TW3Form)
  private
    {$I 'Form1:intf'}
    fLayout: TLayout;
    fHeader: TW3HeaderControl;
    fList: TW3ListBox;
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;


implementation


{ TForm1 }

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  fLayout:= Layout.Client([Layout.Top(fHeader), Layout.Client(fList)]);
end;


procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  fHeader:= TW3HeaderControl.Create(self);
  fList:= TW3ListBox.Create(self);
end;


procedure TForm1.Resize;
begin
  inherited;
  if assigned(FLayout) then
  begin
   fLayout.Resize(self);
    if ClientHeight > ClientWidth then
    fHeader.Height:= ClientHeight Div 6
   else
    fHeader.Height:= ClientHeight Div 8;
  end;
end;


initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

我甚至尝试重写“FormActivated”并将其放在那里并调用Resize。

是什么给出了?

UPDATE !!!!!

而不是在InitializeForm中分配布局

e.g。

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)]);
end;

在Resize方法中指定它而不是

e.g。

procedure TForm1.Resize;
begin
  inherited;
   if ClientWidth > ClientHeight then
    fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6),fHeader), Layout.Client(fList)])
   else
    fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 8),fHeader), Layout.Client(fList)]);
   fLayout.Resize(self);

end;

更新#2

并且,如果我在表单上添加另一个控件,那么还有另一个问题:(

如果我将大小设置为静态值,则效果很好

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}


  fHeader:= TW3HeaderControl.Create(self);
  fHeader.Height:= 50;  //******************************
  fHeader.BackButton.Visible:= False;
  fHeader.Title.Caption:= 'Menu';
  fHeader.Title.AlignText:= taCenter;


  fFooter:= TW3HeaderControl.Create(self);
  fFooter.Height:= 50; //******************************
  fFooter.BackButton.Visible:= False;
  fFooter.Title.Caption:= 'Copyright (C) 2016';
  fFooter.Title.AlignText:= taCenter;


  fList:= TW3ListBox.Create(self);
end;

这是有效的

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  fLayout:= Layout.Client([Layout.Top(fHeader),
                           Layout.Bottom(fFooter),
                           Layout.Client(fList)]);
end;

但是,如果我在运行时根据尺寸动态设置高度,那么它不会

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  fLayout:= Layout.Client([Layout.Top(Layout.Height(ClientHeight Div 6), fHeader),
                           Layout.Bottom(Layout.Height(ClientHeight Div 6),fFooter),
                           Layout.Client(fList)]);
end;

2 个答案:

答案 0 :(得分:3)

这有效

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  fLayout:= Layout.Client([Layout.Top(fHeader),
                           Layout.Bottom(fFooter),
                           Layout.Client(fList)]);
end;

答案 1 :(得分:3)

浏览器对我们施加了一些规则,从对象pascal的角度来看,这似乎很奇怪。 InitializeForm与InitializeObject之间的区别就是其中之一。正如“形式”的怪异一样。

John指出,InitializeForm是个好地方。在构造JS对象并将DOM元素注入对象模型之后调用该方法。这两个不是一回事,这可能有点令人困惑。此外,如果在调用createElement()或构造函数完成后创建元素,则由浏览器决定。因此,我们必须引入一些新的过程来处理这个问题。

您可能希望更接近地调查TApplication。 您将找到以下内容:TApplication - >显示 - >视图。表单在“view”容器中创建。无论何时更改方向或表单大小,最快的通知方式是通过Application.Display.View.OnReSize()事件。

相关问题