如何获得当前屏幕方向?

时间:2022-11-28 08:59:01

I just want to set some flags when my orientation is in landscape so that when the activity is recreated in onCreate() i can toggle between what to load in portrait vs. landscape. I already have a layout-land xml that is handling my layout.

我只是想在我的方向是横向时设置一些标志,以便在onCreate()中重新创建活动时,我可以在纵向加载的内容和横向加载的内容之间进行切换。我已经有了一个layout-land xml,它正在处理我的布局。

public void onConfigurationChanged(Configuration _newConfig) {

        if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            this.loadURLData = false;
        }

        if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            this.loadURLData = true;
        }

        super.onConfigurationChanged(_newConfig);
    }

Over-riding onConfigurationChanged will prevent my layout-land xml from loading in landscape orientation.

对配置的过度控制将阻止我的layouland xml以横向加载。

I just want to get the current orientation of my device in onCreate(). How can I get this?

我只是想在onCreate()中获取设备的当前定位。我怎么能得到这个呢?

8 个解决方案

#1


390  

Activity.getResources().getConfiguration().orientation

#2


40  

int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
    // code for portrait mode
} else {
    // code for landscape mode
}

When the superclass of this is Context

当超类是Context时。

#3


27  

In some devices void onConfigurationChanged() may crash. User will use this code to get current screen orientation.

在某些设备中,配置更改()可能会发生崩溃。用户将使用此代码获得当前屏幕方向。

public int getScreenOrientation()
{
    Display getOrient = getActivity().getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if(getOrient.getWidth()==getOrient.getHeight()){
        orientation = Configuration.ORIENTATION_SQUARE;
    } else{ 
        if(getOrient.getWidth() < getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }else { 
             orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}

And use

和使用

if (orientation==1)        // 1 for Configuration.ORIENTATION_PORTRAIT
{                          // 2 for Configuration.ORIENTATION_LANDSCAPE
   //your code             // 0 for Configuration.ORIENTATION_SQUARE
}

#4


17  

int rotation =  getWindowManager().getDefaultDisplay().getRotation();

this will gives all orientation like normal and reverse

这将提供所有的方向,像正常和反向

and handle it like

和处理它

int angle = 0;
switch (rotation) {
    case Surface.ROTATION_90:
        angle = -90;
        break;
    case Surface.ROTATION_180:
        angle = 180;
        break;
    case Surface.ROTATION_270:
        angle = 90;
        break;
    default:
        angle = 0;
        break;
}

#5


11  

getActivity().getResources().getConfiguration().orientation

this command returns int value 1 for Portrait and 2 for Landscape

此命令返回人像的int值1,风景的int值2

#6


5  

In case anyone would like to obtain meaningful orientation description (like that passed to onConfigurationChanged(..) with those reverseLandscape, sensorLandscape and so on), simply use getRequestedOrientation()

如果任何人想要获得有意义的方向描述(就像传递给onConfigurationChanged(.)方法和这些反向景观、传感器景观等等),只需使用getRequestedOrientation()

#7


1  

In your activity class use the method below :

在您的activity类中使用以下方法:

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            setlogo();// Your Method
            Log.d("Daiya", "ORIENTATION_LANDSCAPE");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

            setlogoForLandScape();// Your Method
            Log.d("Daiya", "ORIENTATION_PORTRAIT");
        }
    }

Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.

然后,要声明您的活动处理配置更改,请在清单文件中编辑适当的元素,以包含android:configChanges属性,该属性的值表示要处理的配置。configChanges属性的文档中列出了可能的值(最常用的值是“orientation”,用于防止屏幕方向改变时重新启动,而“keyboardHidden”用于防止键盘可用性改变时重新启动)。可以在属性中声明多个配置值,方法是使用管道|字符分隔它们。

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

That's all!!

这就是! !

#8


0  

if you want to override that onConfigurationChanged method and still want it to do the basic stuff then consider using super.onConfigyrationChanged() as the first statement in the overriden method.

如果您希望重写onConfigurationChanged方法,并且仍然希望它完成基本的工作,那么请考虑使用super.onConfigyrationChanged()作为overriden方法中的第一个语句。

#1


390  

Activity.getResources().getConfiguration().orientation

#2


40  

int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
    // code for portrait mode
} else {
    // code for landscape mode
}

When the superclass of this is Context

当超类是Context时。

#3


27  

In some devices void onConfigurationChanged() may crash. User will use this code to get current screen orientation.

在某些设备中,配置更改()可能会发生崩溃。用户将使用此代码获得当前屏幕方向。

public int getScreenOrientation()
{
    Display getOrient = getActivity().getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if(getOrient.getWidth()==getOrient.getHeight()){
        orientation = Configuration.ORIENTATION_SQUARE;
    } else{ 
        if(getOrient.getWidth() < getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }else { 
             orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}

And use

和使用

if (orientation==1)        // 1 for Configuration.ORIENTATION_PORTRAIT
{                          // 2 for Configuration.ORIENTATION_LANDSCAPE
   //your code             // 0 for Configuration.ORIENTATION_SQUARE
}

#4


17  

int rotation =  getWindowManager().getDefaultDisplay().getRotation();

this will gives all orientation like normal and reverse

这将提供所有的方向,像正常和反向

and handle it like

和处理它

int angle = 0;
switch (rotation) {
    case Surface.ROTATION_90:
        angle = -90;
        break;
    case Surface.ROTATION_180:
        angle = 180;
        break;
    case Surface.ROTATION_270:
        angle = 90;
        break;
    default:
        angle = 0;
        break;
}

#5


11  

getActivity().getResources().getConfiguration().orientation

this command returns int value 1 for Portrait and 2 for Landscape

此命令返回人像的int值1,风景的int值2

#6


5  

In case anyone would like to obtain meaningful orientation description (like that passed to onConfigurationChanged(..) with those reverseLandscape, sensorLandscape and so on), simply use getRequestedOrientation()

如果任何人想要获得有意义的方向描述(就像传递给onConfigurationChanged(.)方法和这些反向景观、传感器景观等等),只需使用getRequestedOrientation()

#7


1  

In your activity class use the method below :

在您的activity类中使用以下方法:

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            setlogo();// Your Method
            Log.d("Daiya", "ORIENTATION_LANDSCAPE");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

            setlogoForLandScape();// Your Method
            Log.d("Daiya", "ORIENTATION_PORTRAIT");
        }
    }

Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.

然后,要声明您的活动处理配置更改,请在清单文件中编辑适当的元素,以包含android:configChanges属性,该属性的值表示要处理的配置。configChanges属性的文档中列出了可能的值(最常用的值是“orientation”,用于防止屏幕方向改变时重新启动,而“keyboardHidden”用于防止键盘可用性改变时重新启动)。可以在属性中声明多个配置值,方法是使用管道|字符分隔它们。

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

That's all!!

这就是! !

#8


0  

if you want to override that onConfigurationChanged method and still want it to do the basic stuff then consider using super.onConfigyrationChanged() as the first statement in the overriden method.

如果您希望重写onConfigurationChanged方法,并且仍然希望它完成基本的工作,那么请考虑使用super.onConfigyrationChanged()作为overriden方法中的第一个语句。