Android horizontal and vertical screens should solve two problems: one. Layout problem; 2. Reload.
1. layout problems:If you do not want the software to switch between the horizontal and vertical screens, the easiest way is to do so in the AndroidManifest of the project. in xml, add the android: screenOrientation attribute to the activity you specified. It has the following parameters:
"Unspecified"
The default value is used by the system to determine the display direction. The rule is related to the device, so different devices have different display directions.
"Landscape"
Horizontal Screen Display (longer than width)
"Portrait"
Portrait display (height to width)
"User"
User's current preferred direction
"Behind"
Consistent with the direction of the Activity under the Activity (in the Activity stack)
"Sensor"
It is determined by a physical sensor. If you rotate the device, the screen will be switched horizontally and vertically.
"Nosensor"
Ignore the physical sensor so that it will not be changed as the user rotates the device (except for the "unspecified" setting ).
You can also use setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE); in Java code.
If you want the software to switch between portrait and portrait screens, the height and width of the portrait screen may be converted, and different la s may be required. You can switch the layout using the following methods:
1)Create the layout-land and layout-port directories under the res directory. The corresponding layout files remain unchanged, such as main. xml. Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen. If you do not need to worry about it, the simulator will automatically search for it.
2)This. getResources (). getConfiguration (). orientation is used to determine whether the current screen is landscape or landscape and then load the corresponding xml layout file. When the screen changes to a horizontal screen, the system will call the OnCreate method of the current Activity again. You can put the following method in your OnCreate to check the current direction, then, let your SetContentView load different Layout xml.
Copy codeThe Code is as follows: 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.If you do not need to load the data again, you can go to androidmanifest. add android: configChanges = "orientation" to xml. the function of configuring android: configChanges is as described in the document: Specify one or more configuration changes that the activity will handle itself. if not specified, the activity will be restarted if any of these configuration changes happen in the system. In this way, the. Activity will not repeatedly call onCreate () or even onPause. onResume. Only one onConfigurationChanged (Configuration newConfig) will be called ).
-************* In fact, here I encountered two strange problems, that is
1.If I only set orientation in android: configChanges, it will still be reloaded. Only when orientation | keyboardHidden is set will it call only one onConfigurationChanged (Configuration newConfig)
2.When the landscape screen changes to the landscape screen, he will call onConfigurationChanged twice, and when the landscape screen changes to the landscape screen, he only calls onConfigurationChanged once, it is really strange. If you know, leave a message to discuss *************-
If you need to reload data, you do not need to make any modifications. However, if you need to save the previous operation content or data during the reload process, you need to save the previous data. Then it is obtained in onCreate () of the activity. Of course, you cannot set android: configChanges (). Otherwise, the onCreate () method will not be called. Where can the data be stored? Android supports all four storage methods. In addition, Android provides the onRetainNonConfigurationInstance () method for us to temporarily save data.
The following is an example of this operation:
Save temporary image:
Copy codeThe Code is as follows: @ Override
Public Object onRetainNonConfigurationInstance (){
Final LoadedPhoto [] list = new LoadedPhoto [numberOfPhotos];
KeepPhotos (list );
Return list;
}
Then, you can call the temporary file again in the onCreate () function of the activity. In the code, you need to determine whether the system needs to reload the temporary file. The following code loads temporary files in the OnCreate () function:Copy codeThe Code is as follows: 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(cmd.exe cute (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 );
}
}
}
In most cases, you do not need to perform the above operations. Therefore, you must use this article with caution. After all, the best behavior is not applicable to all situations, if the application is poor, it will cause unnecessary troubles to the program.
If you want to completely disable flip, you can set the attribute of android: screenOrientation to nosensor, so you can ignore the troubles caused by gravity sensing. But I don't know why. I don't need to use it on the simulator. I heard people say it is correct on a real machine. I don't have a real machine. I will try again when I have a real machine.