Experience on adaptive screen orientation and size

Source: Internet
Author: User

At the beginning, the simulator I selected was WVGA854 with a resolution of 854*480. After the development, I felt very OK when I installed it on a 800*480 mobile phone, but when I installed it on a 480*320 or 320*240 resolution mobile phone, many interfaces were distorted, at this time, I felt the importance of app self-adaptation.

Adaptive mainly encounters two major problems: switching between the landscape screen and the landscape screen, and the resolution size is different.

1. When the landscape screen is switched to the portrait screen, the solution is as follows:
Create the layout-port and layout-land directories under the res directory, and place the layout files of the portrait and landscape screens respectively, when the mobile phone screen direction changes, the android system automatically calls the corresponding layout file.

Of course, there is another way to avoid switching. Either landscape screen or landscape screen. You can set it in the AndroidManifest. xml file. For example, if an Activity is set to android: screenOrientation = "portrait", the portrait screen is always maintained. If it is set

Android: screenOrientation = "landscape" to keep the landscape.

2. When the resolution varies by hour, we also encounter three problems: switching between the image size, layout, horizontal screen, and vertical screen.

The image problem is solved. In the api after android2.0, we will find that the original drawable folder has changed to three: drawable-hdpi, drawable-mdpi, and drawable-ldpi.

The first folder contains pictures of high-resolution mobile phones, such as 854*480 and 800*480.
The second folder contains pictures of the Resolution mobile phone, for example: 480*320
In the third folder, lower the resolution of the mobile phone image, for example, 320*240.

Similar to switching a layout and landscape to a portrait, you only need to create different layout folders under the res directory, such as layout-480x320, layout-800x480, the system selects the appropriate layout based on the screen size.


When the landscape screen is switched to the portrait screen, a problem occurs. How can I create a layout folder?
Is all layout folders in my project:

Layout is a layout folder for high-resolution vertical screens.
Layout-land is a layout folder for high-resolution landscape screens.
The layout-land-320x240 is a layout folder that adapts to low-resolution landscape screens
The layout-land-480x320 is the layout folder that adapts to the in-resolution landscape Screen
The layout-port-320x240 is a layout folder that adapts to low-resolution vertical screens
The layout-port-480x320 is the layout folder that adapts to the in-resolution vertical screen

This completes the adaptation of the layout file.
I personally think it is better to develop a high-resolution simulator first, and then adapt to different resolutions. Sometimes we may need to modify the low-resolution layout.

Finally, what should I do if I write a dead layout in a java program?
This is annoying. You need to determine the screen size. The code for getting the screen size is as follows: Java code
 

  1. WindowManager windowManager = getWindowManager ();
  2. Display display = windowManager. getDefaultDisplay ();
  3. Int screenwidth = display. getwidth ();
  4. Int screenheight = display. getheight ();
WindowManager windowManager = getWindowManager (); Display display = windowManager. getdefadisplay display (); int screenWidth = display. getWidth (); int screenHeight = Display. getHeight ();

The following code snippet is used by me to write dead layout in the program. It is for reference only: Java code
 

  1. Button cancelbtn = new button (this );
  2. If (screenwidth <320 | screenheight <320)
  3. Cancelbtn. setlayoutparams (New layoutparams (60,
  4. Android. View. viewgroup. layoutparams. wrap_content ));
  5. Else if (screenWidth <480 & screenHeight = 480)
  6. CancelBtn. setLayoutParams (new LayoutParams (80,
  7. Android. view. ViewGroup. LayoutParams. WRAP_CONTENT ));
  8. Else if (screenWidth> 480 & screenHeight = 480)
  9. CancelBtn. setLayoutParams (new LayoutParams (160,
  10. Android. View. viewgroup. layoutparams. wrap_content ));
  11. Else
  12. Cancelbtn. setlayoutparams (New layoutparams (120,
  13. Android. View. viewgroup. layoutparams. wrap_content ));
Button cancelBtn = new Button (this); if (screenWidth <320 | screenHeight <320) cancelBtn. setLayoutParams (new LayoutParams (60, android. view. viewGroup. layoutParams. WRAP_CONTENT); else if (screenWidth <480 & screenHeight = 480) cancelBtn. setLayoutParams (new LayoutParams (80, android. view. viewGroup. layoutParams. WRAP_CONTENT); else if (screenWidth> 480 & screenHeight = 480) cancelBtn. setLayoutParams (new LayoutParams (160, android. view. viewGroup. layoutParams. WRAP_CONTENT); elsecancelBtn. setLayoutParams (new LayoutParams (120, android. view. viewGroup. layoutParams. WRAP_CONTENT ));

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.