在Android中更改特定页面的方向 [英] Changing orientation of a particular page in android

查看:143
本文介绍了在Android中更改特定页面的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Qt和C ++中开发一个Android应用程序.我的整个应用程序都是纵向的,但是当我播放视频时,我想将方向更改为横向,并且在视频结束后应该再次更改为纵向.

I am working on an android app in Qt and c++. My whole application has portrait orientation.But when i play a video i want to change the orientation to landscape, and after video ends it should again change to portrait.

所以问题是如何在Android的Qt/C ++应用程序中将屏幕设置为横向或纵向模式.

So the question is How is it possible to set the screen to landscape or portrait modes in a Qt/C++ application for Android.

推荐答案

可以使用 setRequestedOrientation Java函数更改Android上的屏幕方向,因此您应该从应用程序中调用Java函数.要在您的Qt Android应用程序中运行Java代码,您应该使用 Qt Android Extras 模块,其中包含用于在Android上进行开发的其他功能.

Screen orientation on Android can be changed using setRequestedOrientation Java function so you should call a Java function from your app. To run Java code in your Qt Android application you should use the Qt Android Extras module which contains additional functionality for development on Android.

您可以使用JNI从C/C ++调用Java函数或从Java回调C/C ++函数.

You can use JNI to call a Java function from C/C++ or callback a C/C++ function from Java.

在这里,您可以在静态Java方法中使用它:

Here you could have it in a static Java method like :

package com.MyApp;

public class OrientationChanger
{
    public static int change(int n)
    {
        switch(n)
        {
               case 0:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                   break;
               case 1:
                   setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                   break;                   
           }
    }
}

首先,您需要将其添加到您的.pro文件中:

First you need to add this to your .pro file :

QT += androidextras

并包括相关的头文件:

#include <QAndroidJniObject>

然后可以从C ++代码调用此静态Java函数.

You can then call this static Java function from your C++ code.

要将方向更改为横向模式:

To change the orientation to landscape mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 0);

要将方向更改为纵向模式:

To change the orientation to portrait mode :

bool retVal = QAndroidJniObject::callStaticMethod<jint>
                        ("com/MyApp/OrientationChanger" // class name
                        , "change" // method name
                        , "(I)I" // signature
                        , 1);

这篇关于在Android中更改特定页面的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆