Android screen to solve the problem should be two: one. Layout problems; Reload the problem.
1. Layout problem: If you do not want the software to switch between the screen, the simplest way is to find in the project Androidmanifest.xml you specify the activity in addition to the Android:screenorientation attribute, he has the following parameters:
"Unspecified"
The default value is determined by the system to determine the direction of the display. The decision strategy is related to the device, so different devices will have different display directions.
"Landscape"
Horizontal screen display (width ratio to long)
"Portrait"
Vertical display (high width to long)
"User"
The user's current preferred orientation
"Behind"
is in the same direction as the activity below the activity (in the activity stack)
"Sensor"
There are physical sensors to decide. If the user rotates the device, the screen will switch between the screens.
"Nosensor"
The physical sensor is ignored so that it does not change as the user rotates the device (except for the "unspecified" setting).
You can also set it in Java code by setrequestedorientation (Activityinfo.screen_orientation_landscape).
If you want the software to switch between the screens and the screen, it may require a different layout because the aspect of the screen will change. You can switch layouts in the following ways:
1) in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout file unchanged, such as Main.xml. Layout-land is the horizontal screen of the layout,layout-port is the vertical screen layout, the other without the tube, the simulator will automatically find.
2) by This.getresources (). GetConfiguration (). Orientation to determine whether the screen is currently horizontal or vertical and then load the corresponding XML layout file. Because when the screen changes to a horizontal screen, the system will re-call the current activity's OnCreate method, you can put the following methods in your oncreate to check the current direction, and then you can let 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 the problem. If you do not need to reload, you can add the configuration android:configchanges= "orientation" in Androidmanifest.xml to configure Android: Configchanges's role is as the document says: Specify one or more configuration changes that the activity would handle itself. If not specified, the activity would be restarted if any of the these configuration changes happen in the system. This is in the program. The activity will not repeat the call to OnCreate () and will not even call Onpause.onresume. Only one onconfigurationchanged (Configuration newconfig) is called.
************* actually here I have two strange problems, that is
1. If I only set orientation in Android:configchanges, he will still reload, only set the orientation| Keyboardhidden it only calls a onconfigurationchanged (Configuration newconfig)
2. When the horizontal screen into the vertical screen, he will call two times onconfigurationchanged, and vertical screen to the horizontal screen when he only call once onconfigurationchanged, it is very strange. If you know, welcome message to discuss ************* together?
If re-loading is required, no modifications are required. However, if you need to save the previous action content or data during the reload process, you need to save the previous data. It is then removed from the activity's onCreate (). Of course, android:configchanges () cannot be set, otherwise the OnCreate () method will not be called. So where can the data be stored? There are four storage methods available in Android, and you can also use Android to provide us with the onretainnonconfigurationinstance () method to temporarily save the data.
Here is an example:
To save a temporary picture:
@Override
Public Object onretainnonconfigurationinstance () {
Final loadedphoto[] list = new Loadedphoto[numberofphotos];
Keepphotos (list);
return list;
}
You can then implement a recall of the temporary file in the activity's OnCreate () function, in which you need to determine whether the system needs to reload the temporary file. Here's the code to load the temp 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);
}
}
}
For most situations there is no need to do the above, so use this wording with caution, after all, the best behavior does not apply to all situations, but if the application is not good it will cause unnecessary trouble to the program.
If you want to completely prohibit flipping, you can set the Android:screenorientation property to Nosensor, so you can ignore the trouble of gravity induction. But do not know why, in the simulator does not work, listen to others said on the real machine is correct, I do not have a real machine, and so have a real machine to try again.
Transferred from: http://java-admin.javaeye.com/blog/730863