Horizontal screen switching for Android

Source: Internet
Author: User

1. Switch the horizontal and vertical screen mode of the simulator:Shortcut: Ctrl + F12

If the screen is fixed, run dialog-> Target in eclipse can be set. If it is a command line, you can use parameters.
Emulator-skin HVGA-L

 

2.ProgramThe interface remains in one direction and does not change with the orientation of the mobile phone:

Configure androidmanifest. xml. Add this line to Android: screenorientation = "Landscape ".

For example, (landscape is horizontal and portrait is vertical ):

In addition, the activity is restarted every time the screen is switched on in Android. Therefore, you should save the status of the current activity before the activity is destroyed and load the configuration when the activity is created again, in progress, the game will not automatically restart!
Some programs are suitable for switching from a portrait screen to a landscape screen, or vice versa. What should we do at this time? You can configure Android: screenorientation = "portrait" in the activity configuration area ". In this way, you can ensure that the portrait screen is always portrait or landscape.

 

 

3. In the program, you can change the style of the screen, such as the background image, button size, and layout. Therefore, you can use the Android system to restart the activity feature every time you switch the screen, and automatically load layout in different display states at oncreat.

There are several implementation methods:

(1) manually convert resource files and other operations. This is because oncreate () is called after the screen is changed and restarted again, so you can change the resource file after determining whether the current screen is in the horizontal or vertical direction. The method is as follows:
// Obtain the current screen direction public static int screenorient (activity) {// obtain the current screen direction. If this value is-1, androidmanifest is returned. the Android: screanorentation attribute is not set in XML, so the screen direction cannot be determined. // You can use another method, that is, a landscape screen with a length greater than the height; otherwise, a landscape screen is used. Int orientation = activity. getrequestedorientation (); // get the int landscape = activityinfo in the screen direction. screen_orientation_landscape; // static constant int portrait = activityinfo. screen_orientation_portrait; // The vertical screen constant int width = activity. getwindowmanager (). getdefadisplay display (). getwidth (); // obtain the system display attribute and the screen width is int Height = activity. getwindowmanager (). getdefadisplay display (). getheight (); // get the screen height. Return width> height? Portrait: Landscape; // judgment}

(2) To prevent the Android system from automatically restarting the activity, you must go to androidmanifest. add the Android: configchanges = "keyboardhidden | orientation" attribute to the corresponding activity in XML to restart the activity and call onconfigurationchanged (configuration newconfig ).

The onconfigurationchanged () method can also dynamically change the resource file and maintain the previous state. In this way:
 
@ Overridepublic void onconfigurationchanged (configuration newconfig) {super. onconfigurationchanged (newconfig); If (this. getresources (). getconfiguration (). orientation = configuration. orientation_landscape) {log. D ("= landscape =", "= landscape ="); setcontentview (R. layout. main);} else if (this. getresources (). getconfiguration (). orientation = configuration. orientation_portrait) {log. D ("= portrait =", "= portrait ="); setcontentview (R. layout. main2 );}}
In this mode, the android system automatically looks for different interface files, while androidmanifest. android: configchanges = "keyboardhidden | orientation" has been defined in XML, so we only need to change the listener direction in onconfigurationchanged.

Note: QQ technology exchange group: add one if you are interested in 108614806.

 

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.