How does Android adapt to multiple screen mobile phones?

Source: Internet
Author: User

I have been in touch with Android for a while. I personally feel that the android page self-adaptation is much more complicated than the web. Because Mobile Phone development requires multiple screens, I will summarize the following: different layoutAndroid mobile phones have different screen sizes, including 480x320,640x360,800 x480. how can I enable the Application to automatically adapt to different screens? In fact, it is very simple, just need to create different layout folder under the res directory, such as layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size. Ii. In earlier versions, hdpi, mdpi, and ldpi only have one drawable, while in version 2.1, there are three drawable-mdpi, drawable-ldpi, and drawable-hdpi, these three methods are mainly used to support multi-resolution. Drawable-hdpi, drawable-mdpi, drawable-ldpi differences: (1) drawable-hdpi stores high-resolution images, such as WVGA (480x800), FWVGA (480x854) (2) drawable-mdpi stores medium-resolution images, such as HVGA (320x480) (3) drawable-ldpi stores low-resolution images, such as QVGA (240x320) the system will find the corresponding images in these folders according to the machine resolution. To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed. 3. During Game Development, some games can only be played on the horizontal screen. Therefore, when the mobile phone is erected and placed, the game screen must be kept on the horizontal screen. To do this, you can simply configure it in AndroidManifest. xml. Add this line to android: screenOrientation = "landscape ". For example, (landscape is horizontal and portrait is vertical): Java code <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. ray. linkit "android: versi> <application android: icon =" @ drawable/icon "android: label =" @ string/app_name "> <activity android: name = ". main "android: label =" @ string/app_name "android: screenOrientation =" portrait "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" andro Id. intent. category. LAUNCHER "/> </intent-filter> </activity> <activity android: name = ". gamePlay "android: screenOrientation =" portrait "> </activity> <activity android: name = ". optionView "android: screenOrientation =" portrait "> </activity> </application> <uses-sdk android: minSdkVersion =" 3 "/> </manifest> 1. 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! 2. you can also add the android: c attribute to each activity without restarting the activity. instead, call onConfigurationChanged (Configuration newConfig ). in this way, you can adjust the display mode in this method. for example, Java code if (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE) {// transverse setContentView (R. layout. file_list_landscape);} else {// vertical setContentView (R. layout. file_list);} when developing android or Ophone, by default, the onConfigurationChanged event is triggered when the screen is changed from the vertical evaluation to the horizontal screen. By default, the screen is reloaded and the same screen as the horizontal evaluation is displayed. There are two problems, layout Problem, displayed on the portrait Screen The layout of the horizontal screen will affect the layout because of the variation of width and height (unless you create two sets of images and then add a judge how to display them on the horizontal screen, how to display the image on the portrait screen), of course, the simplest way is to use the AndroidManifest in the project. in xml, if you find that only the portrait screen is displayed in the activity you specified (android: screenOrientation = "portrait"), only the landscape screen is displayed (android: screenOrientation = "landscape ") picture re-loading problem: by default, the screen will be re-loaded during horizontal and vertical switching, leading to unnecessary resource waste, and more serious is the data maintained on the screen (especially for Games) it also has been reset (of course you can save the data to the database or to a text file before the reset), how to avoid re-loading the screen during horizontal/vertical switching, first in AndroidManifest. in xml, add android: configChanges = "orienta Tion | keyboardHidden "and then reload the onConfigurationChanged event in the logic processing (Code part) of the activity. For details about the code, refer to: @ Override public void onConfigurationChanged (Configuration config) {super. onConfigurationChanged (config);} above, you can easily control the display mode of your app screen and will not reload the screen when switching between the horizontal and vertical Screens without changing the display mode, that is, your app is optimized and the user experience is improved accordingly.

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.