Android training-Supports different screens

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/supporting-devices/screens.html

Android uses two attributes to classify the screen of a device: size and density. You should imagine the device screen size and density range for installing your app. For example, you should include some optional resources to optimize the display appearance of your applications on screens of different sizes and density.

1. Four general dimensions: small, normal, large, and xlarge

2. Four General density types: low (ldpi), medium (mdpi), high (hdpi), extra high (xhdpi)

For la S and bitmaps used by different screens, you must put them in a separate directory, similar to multi-language support.

Pay attention to the changes to the screen size caused by the screen direction (horizontal or vertical screen). Therefore, many applications need to modify the layout to optimize the user experience in each direction.

Create different la s

To optimize the user experience on different screen sizes, you should provide a unique layout XML file for each screen size idea to be supported. Each layout file should be saved in its corresponding resource directory. The directory name should use _ <screen_size> as the suffix. For example, the unique layout for the large screen should be saved in the Res/layout_large/directory.

Note:To properly fill the screen, Android automatically scales your layout. In this way, the layout of different screen sizes does not have to worry about the absolute size of the UI elements, but focuses on the layout structure that affects user experience (for example, the size or position of an important view is relative to that of a sibling view)

For example, a project that contains the default layout and an optional layout for the large screen:

Myproject/


Res/


Layout/


Main. xml


Layout-large/


Main. xml

The layout file names must be identical, but their content is different to provide the UI of the corresponding screen size.

As usual, simply layout the file in your application:

@ Override

Protected void oncreate (bundle savedinstancestate ){


Super. oncreate (savedinstancestate );


Setcontentview (R. layout. Main );

}

The Android system loads the corresponding layout File Based on the screen size of the device that runs your application. For more information, see "Providing
Resources guide.

 

The following example shows an optional layout for the horizontal screen:

MyProject/
    res/
        layout/
            main.xml
        layout-land/
            main.xml

By default, layout/Main. XML is used for vertical layout.

 

If you want to provide a special layout for the portrait and apply it to the large screen, you need to use the large and land modifiers:

MyProject/
    res/
        layout/              # default (portrait)
            main.xml
        layout-land/         # landscape
            main.xml
        layout-large/        # large (portrait)
            main.xml
        layout-large-land/   # large landscape
            main.xml

Note: android3.2 and later support an advanced method for defining screen sizes, which allows you to specify resources for screen sizes using the width and height in density-independent pixel terms. This lesson does not contain this new technology. For more information, see: Designing
For multiple screens.

Create different bitmap

You should always provide the right bitmap resources for each General density: low, medium, high, and extra-high. This helps achieve good image quality and performance in all screen density.

To generate these images, you should use the original resources in vector format and generate images of each density according to the following scaling ratio:

1. xhdpi: 2.0

2. HD Pi: 1.5

3. mdpi: 1.0 (baseline)

4. ldpi: 0.5

This means that if you want to generate an image of 150x150 for the xhdpi device, the same resource will also generate images of the following sizes: 100x100 for hdpi; X for mdpi; 75x75 for ldpi.

Then they are placed in the corresponding directory of the plotting Resource:

MyProject/
    res/
        drawable-xhdpi/
            awesomeimage.png
        drawable-hdpi/
            awesomeimage.png
        drawable-mdpi/
            awesomeimage.png
        drawable-ldpi/
            awesomeimage.png

Whenever @ drawable/awesomeimage is referenced, the system selects the corresponding Bitmap Based on the screen density.

Note: ldpi resources are not always necessary. When you provide hdpi resources, the system will halved them to fill the ldpi screen.

For instructions on creating icon assets for applications, see iconography design guide.

 

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.