How does Android adapt to multiple screen mobile phones?

Source: Internet
Author: User

Thank you !!!

Reprinted Please note: http://blog.csdn.net/richway2010/archive/2011/06/29/6574987.aspx

Bloggers: All bloggers, netizens, and everyone on the Internet! Welcome to this blog. You are welcome to have more exchanges, give more comments, learn from each other, and make mutual progress. Our slogan is: study hard and make progress every day .]

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:
I. Different Layout
The screen sizes of Android mobile phones vary by 480X320,640x360,800 X. 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. hdpi, mdpi, and ldpi
In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.
Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:
(1) The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and 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. Landscape screen Switching
During Game Development, some games can only be played on a horizontal screen. Therefore, when mobile phones are erected and placed, the game screen must be kept on a horizontal screen. It is actually very easy to achieve this requirement,
Configure androidmanifest. xml. Add this line to Android: screenorientation = "Landscape ".

For example, (landscape is horizontal and portrait is vertical ):
Code
 <? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. ray. linkit "<br/> Android: versi> <br/> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <br/> <activity Android: Name = ". main "<br/> Android: Label =" @ string/app_name "<br/> Android: screenorientation = "portrait"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". gameplay "<br/> Android: screenorientation =" portrait "> </activity> <br/> <activity Android: Name = ". optionview "<br/> Android: screenorientation =" portrait "> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "3"/> <br/> </manifest>
1. In addition, the activity is restarted every time the screen is switched on in Android. Therefore, the status of the current activity should be saved before the activity is destroyed,
Load the configuration when the activity creates again, so that the ongoing game will not automatically restart!
2. You can add the Android: C attribute to each activity so that the activity will not be restarted. Instead, you can 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 ){
// Horizontal
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 problems, the layout displayed on the portrait screen is changed to the landscape screen because of the width and height. All the changes will affect the layout (unless you create two sets of scenes and then add a judge how to display them on the Landscape screen, how to display the image on the portrait screen), of course, the simplest way is to use the androidmanifest in the project. find the activity you specified in XML and add it

    • If only the portrait screen is displayed (Android: screenorientation = "portrait ")
    • For horizontal screen display only (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, you must find Android: configchanges = "orientation | keyboardhidden" in your specified activity in androidmanifest. xml"
    • Reload the onconfigurationchanged event in the logic processing of the activity (in the Code Section). For details about the code, refer:
      • @ Override
        Public void onconfigurationchanged (configuration config ){
        Super. onconfigurationchanged (config );
        }

You can simply control the display mode of your app screen and the screen will not be reloaded when the display mode remains unchanged, that is, your app is optimized and the user experience is improved accordingly.

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.