The Android system uses two common properties: size and density to classify the device screen. You need to anticipate what screen devices your app will be installed on, including screen size and density. In this case, you will need to provide some optional resource classes to give your app the best display on different screen devices.
- There are four kinds of universal sizes: small, normal, large, xlarge
- There are also four common densities: low (LDPI), Medium (MDPI), High (hdpi), Extra High (xhdpi)
To declare different layouts and pictures for different screens, you need to put these optional resources in different directories, similar to how you can support multiple languages.
Also take into account the screen orientation configuration under different screen sizes, so many applications change different layouts in different directions to achieve the best user experience.
Create a different layout
Let your app have a good user experience on different screen sizes, and you should create a unique XML layout file for each screen size you want to support. Each layout file should be stored in the appropriate resource directory with the directory name -<screen_size> as a suffix. For example, the name of a layout directory that holds a large screen is res/layout-large/ .
Tips: The Android system automatically scales the layout to properly fit the screen. So you don't have to worry about the actual size of the UI elements you're designing for different layouts, you need to be aware of the layout structure that affects the user experience (for example, the size and location of the layout next to the important layout).
For example, this project includes a default layout directory and a layout directory for a large screen:
myproject/ res/ layout/ main.xml layout-large/ main.xml
The filenames must be exactly the same, but their content is not the same, to provide the best UI presentation for different screen sizes.
Simply refer to the layout in your app:
@Override protected voidonCreate(Bundlesavedinstancestate) { Super.onCreate(savedinstancestate);Setcontentview(R.Layout.Main);}
The system will add a layout file to the appropriate layout directory based on the device's screen size. For more information on choosing the appropriate layout resource, see Providing Resources guide.
For another example, here is an optional resource for the project's horizontal screen:
myproject/ res/ layout/ main.xml layout-land/ main.xml
By default, layout/main.xml files are used by the vertical screen.
If you want to assign a horizontal screen layout to your app, including on a large screen device, you need to use large and land limit both:
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
Tips: Android 3.2 and higher systems provide an advanced method for defining screen size, which allows you to specify the screen size of resources based on the minimum width and height of density-independent pixels. This course does not include this new technology. For more information, see Designing for multiple Screens.
Create a different picture
You should provide a picture resource that scales correctly for each universal density device: Low, Medium, high and extra-high density. This will help you get higher picture quality and better performance on all screen density devices.
To generate these images, you first need to use the following size to generate images for different screen densities for your native resource's vector resources:
- xhdpi:2.0
- hdpi:1.5
- mdpi:1.0 (Baseline)
- ldpi:0.75
This means that if you generate a 200x200 image for a xhdpi device, you should generate 150x150 of the same resources for HDPI devices MDPI device 100x100,ldpi device 75x75.
These files are then placed in the appropriate drawable resource directory:
myproject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png drawable-ldpi/ awesomeimage.png
Each time you quote @drawable/awesomeimage , the system selects the appropriate image based on the screen density.
Tip: Low-resolution (LDPI) resources are not necessary. When you provide HDPI resources, the system shrinks the resources in half to properly fit the ldpi screen.
For more information on creating an icon resource for your app, see Iconography Design Guide.