Android实例-Delphi在运行时更改Android屏幕旋转(IOS也支持,不过我可没有苹果机,测试不了)

十年热恋 提交于 2020-04-08 11:51:35

相关资料:

https://www.it1352.com/624177.html

 

android实例:

unit Unit2;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMXTee.Engine,
  FMXTee.Procs, FMXTee.Chart, FMX.Layouts, FMX.Controls.Presentation,
  FMX.StdCtrls;

type
  TForm2 = class(TForm)
    Layout1: TLayout;
    Chart1: TChart;
    Layout2: TLayout;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation
uses
  FMX.Platform; //需要引入
{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

procedure TForm2.Button1Click(Sender: TObject);
var
  ScreenService: IFMXScreenService;
  OrientSet: TScreenOrientations;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService))
  then
  begin
    OrientSet := [TScreenOrientation.soLandscape];
    ScreenService.SetScreenOrientation(OrientSet);
  end;
end;

procedure TForm2.Button5Click(Sender: TObject);
var
  ScreenService: IFMXScreenService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService))
  then
  begin
    case ScreenService.GetScreenOrientation of
      TScreenOrientation.Portrait: ShowMessage('Portrait');
      TScreenOrientation.Landscape: ShowMessage('landscape');
      TScreenOrientation.InvertedPortrait: ShowMessage('Inverted-Portrait');
      TScreenOrientation.InvertedLandscape: ShowMessage('Inverted-Landscape');
      else ShowMessage('not set');
    end;
  end;
end;

end.
View Code

 

 IOS实例(我没有苹果机,没测试过):

 1 Uses: iOSapi.UIKit; 
 2 
 3 procedure ChangeiOSorientation(toOrientation: UIInterfaceOrientation;
 4 possibleOrientations: TScreenOrientations);
 5 var
 6     win : UIWindow;
 7     App : UIApplication;
 8     viewController : UIViewController;
 9     oucon: UIViewController;
10 begin
11     Application.FormFactor.Orientations := []; //Change supported orientations
12     App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication);
13     win := TUIWindow.Wrap(App.windows.objectAtIndex(0)); //The first Windows is always the main Window
14 
15     App.setStatusBarOrientation(toOrientation);
16     {After you have changed your statusbar orientation set the
17     Supported orientation/orientations to whatever you need}
18     Application.FormFactor.Orientations := possibleOrientations;
19     viewController := TUIViewController.Wrap(TUIViewController.alloc.init);//dummy ViewController
20     oucon := TUIViewController.Wrap(TUIViewController.alloc.init);
21     {Now we are creating a new Viewcontroller now when it is created
22      it will have to check what is the supported orientations}
23     oucon := win.rootViewController;//we store all our current content to the new ViewController
24     Win.setRootViewController(viewController);
25     Win.makeKeyAndVisible;// We display the Dummy viewcontroller
26 
27     win.setRootViewController(oucon);
28     win.makeKeyAndVisible; 
29     {And now we Display our original Content in a new Viewcontroller 
30      with our new Supported orientations} 
31 end;
View Code

调用:

1  ChangeiOSorientation(UIInterfaceOrientationPortrait,[TScreenOrientation.Portrait,TScreenOrientation.Landscape,TScreenOrientation.InvertedLandscape]); 
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!