android judge the horizontal and vertical screen problem

  • 2020-05-10 18:52:03
  • OfStack

Android horizontal portrait should solve only two problems: 1. Layout problem; 2. Reload problem.
1. Layout problem: if you don't want the software to switch between horizontal and vertical screens, the easiest way is to find the android:screenOrientation property in the AndroidManifest.xml of the project.
"unspecified"
The default value is determined by the system for the display direction. The policy determined is device-related, so different devices will have different display directions.
"landscape"
Landscape display (width is longer than height)
"portrait"
Portrait display (height over width)
"user"
The user's current preferred direction
"behind"
1 to the direction of the Activity below the Activity (in the Activity stack)
"sensor"
There are physical sensors to determine that. If the user rotates the device this screen will switch horizontally and vertically.
"nosensor"
Ignore the physical sensor so it doesn't change as the user rotates the device (except for the "unspecified" setting).
You can also use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in the Java code. To set up.
If you want the software to switch between horizontal and vertical screens, you may need a different layout because the height and width of the horizontal and vertical screens will be changed. You can switch layouts by:

1) create the layout-land and layout-port directories in the res directory, and the corresponding layout files will remain unchanged, such as main.xml. layout-land is layout for landscape, layout-port is layout for portrait, whatever else, the simulator will find it automatically.

2) determine whether it is landscape or portrait screen by this.getResources ().getConfiguration ().orientation and then load the corresponding xml layout file. Because when the screen changes to landscape, the system will call the current Activity OnCreate method again. You can put the following method in your OnCreate to check the current direction, and then you can ask your SetContentView to load different Layout xml.


if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
Log.i("info", "landscape"); 
} 
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
Log.i("info", "portrait"); 
}

2. Reload problem. If you do not need to reload, you can add the configuration android:configChanges="orientation" to androidmanifest.xml. The configuration android:configChanges does exactly what the documentation says: Specify one or more configuration changes the activity will handle itself If not specified the activity be restarted if any of configuration changes happen in the the system In this way, in the program. Activity will not call onCreate() repeatedly or even onPause.onResume. Only one onConfigurationChanged(Configuration newConfig) will be called.
************* ****
1. If I only set orientation in android:configChanges, it will still reload, and only if orientation|keyboardHidden will it only call 1 onConfigurationChanged(Configuration newConfig).

2. When landscape changes to portrait, he calls onConfigurationChanged twice. When landscape changes to landscape, he calls onConfigurationChanged only once. If you know, please feel free to leave a comment to discuss the ***********
If you need to reload, you don't need to make any changes. However, if you need to save the previous action or data during a reload, you need to save the previous data. And then pull it out of onCreate() of activity. Of course, you cannot set android:configChanges(), otherwise the onCreate() method would not be called. So where can the data be stored? All 4 storage methods in Android can be used, in addition, Android provides us with onRetainNonConfigurationInstance() method to temporarily save data.
Here's an example:
Save temporary images:


@Override 
public Object onRetainNonConfigurationInstance() {  
final LoadedPhoto[] list = new LoadedPhoto[numberOfPhotos];  
    keepPhotos(list);  
return list;  
}  

The temporary file can then be recalled from activity's onCreate() function, and the code needs to determine whether the system needs to reload the temporary file. Here is the code to load the temporary file in the OnCreate() function:

private void loadPhotos() {  
final Object data = getLastNonConfigurationInstance();  

// The activity is starting for the first time, load the photos from Flickr 
if (data == null) {  
        mTask = new GetPhotoListTask().execute(mCurrentPage);  
    } else {  
// The activity was destroyed/created automatically, populate the grid 
// of photos with the images loaded by the previous activity 
final LoadedPhoto[] photos = (LoadedPhoto[]) data;  
for (LoadedPhoto photo : photos) {  
            addPhoto(photo);  
        }  
    }  
}  

You don't need to do the above for most situations, so you should use this line with caution. After all, the best behavior is not suitable for all situations. If you don't use it well, you will cause unnecessary trouble to the program.

If you want to ban flipping completely, you can set android:screenOrientation to nosensor, so you can ignore the gravity sensor. But I don't know why, it doesn't work in the simulator, I heard that it is correct on the real machine, I don't have a real machine, I will try again when I have a real machine.


Related articles: