Android determine screen orientation at runtime

后端 未结 6 729
清酒与你
清酒与你 2021-02-01 05:41

Here\'s a pseudo code to detect screen rotate event, and decide to retain or changes the screen orientation.

public boolean onOrientationChanges(orientation) {
          


        
6条回答
  •  一向
    一向 (楼主)
    2021-02-01 05:56

    Try running

    getResources().getConfiguration().orientation
    

    From your context object to figure out what is the screen orientation at runtime, the possible values are documented here

    In order to catch the orientation change event you can find the answer in the Android Dev Guide: Handling the Configuration Change Yourself

    From the guide :

    For example, the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name">

    Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

    ...

提交回复
热议问题