The Android system uses two common properties: size and density to classify the device screen.
You'll need to pre-measure what screen device 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 have these optional resources in different folders, similar to how you can support multiple languages.
The same takes 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 under the appropriate resource folder. The folder name -<screen_size>
is the suffix.
For example, the name of a layout folder that holds a large screen is res/layout-large/
.
Tips: The Android system will actively scale 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 (such as the size and location of the layout that's next to the important layout).
For example, this project contains a default layout folder and a layout folder for a large screen:
myproject/ res/ layout/ main.xml layout-large/ main.xml
The file names must be exactly the same, but their contents are not the same. Used 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 the layout file under the appropriate layout folder according to the screen size of the device. For more information on selecting the appropriate layout resources, please refer to providing resource guide.
As an example, the following 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.
Suppose you want to specify a horizontal screen layout for your app, which is included on a large screen device, then you need to use and restrict it at the same time large
land
:
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 way to define the size of the screen. It agrees with the minimum width and height of the pixel in terms of density-independent pixels, to specify the screen size of the resource.
This course does not cover this new technology. A lot of other information. Please refer to designing for multiple Screens.
Create a different picture
You should provide a picture resource that can be scaled 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 the same resources 150x150 for HDPI devices MDPI device 100x100,ldpi device 75x75.
These files are then placed under the appropriate Drawable resource folder:
myproject/ res/ drawable-xhdpi/ awesomeimage.png drawable-hdpi/ awesomeimage.png drawable-mdpi/ awesomeimage.png drawable-ldpi/ awesomeimage.png
Every time you quote @drawable/awesomeimage
. The system will select 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.
Many other wizards about creating icon resources for your app. Please refer to iconography Design Guide.
Android Learning Route (18) support for different devices--support different screens