Summary of Android horizontal and vertical screen Switching

Source: Internet
Author: User

Android horizontal and vertical screens should solve two problems: one. Layout problem; 2. Reload.

1. layout Problem: if you do not want to allow the software to switch between the horizontal and vertical screens, the simplest way is 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 a horizontal screen or a vertical screen 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.
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:

1. @ Override
2. public Object onRetainNonConfigurationInstance (){
3. final LoadedPhoto [] list = new LoadedPhoto [numberOfPhotos];
4. keepPhotos (list );
5. return list;
6 .}

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:

1. private void loadPhotos (){
2. final Object data = getLastNonConfigurationInstance ();
3.
4. // The activity is starting for the first time, load the photos from Flickr
5. if (data = null ){
6. mTask = new getphotolisttask(cmd.exe cute (mCurrentPage );
7.} else {
8. // The activity was destroyed/created automatically, populate the grid
9. // of photos with the images loaded by the previous activity
10. final LoadedPhoto [] photos = (LoadedPhoto []) data;
11. for (LoadedPhoto photo: photos ){
12. addPhoto (photo );
13 .}
14 .}

15 .}

 

In Android, when the screen is switched horizontally or vertically, the lifecycle of the Activity is re-loaded (indicating that the current Activity is destroyed but loaded again, what if the current Activity is not destroyed?

1. Set the configChanges attribute for the Activity IN AndroidManifest. xml,


Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". MainActivity"
Android: label = "@ string/app_name" android: configChanges = "orientation | keyboardHidden">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

ConfigChanges has the following options: 1. orientation: the screen rotates vertically and horizontally. keyboardHidden: displays or hides the keyboard. fontScale: the user changed the preferred font size. 4. locale: You have selected different language settings. keyboard: the keyboard type is changed. For example, the mobile phone is switched from 12 to the full keyboard. 6. touchscreen or navigation: the keyboard or navigation mode changes,

If the keyboardHidden option is missing, the Activity cannot be destroyed. In the onConfigurationChanged event mentioned later, only the vertical screen to horizontal screen can be captured.
2. Rewrite the onConfigurationChanged method in the corresponding Activity:

 

Public class MainActivity extends Activity {
Private TextView textView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. I ("-- Main --", "onCreate ");
TextView = (TextView) findViewById (R. id. TV _id );
}


@ Override
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
Log. I ("-- Main --", "onConfigurationChanged ");
If (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE ){
TextView. setText ("current screen is landscape ");
} Else {
TextView. setText ("the current screen is a portrait screen ");
}
}

}

 


From Turing's dream

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.