Supporting Different Screens
Android uses two common properties to classify device screens: Size and pixel density. As a developer, you should expect that your android application will be installed on devices of different screen sizes and pixel density. Therefore, your application should contain optional resources to optimize the appearance of the application to support devices of different sizes and pixel density.
- * Four general dimensions are available: small, normal, large, and xlarge.
- * There are also four broad pixel density types: low (ldpi), medium (mdpi), high (hdpi), and extra high (xhdpi ).
To declare different layouts and bitmaps resources for different screens, you must place these optional resource files in different directories, which is similar to the strings operation in different languages when you are internationalization.
In addition, landscape or portrait is also considered as a screen size change, therefore, most applications should modify the layout to improve the user experience in different screen directions.
Create Different Layout files-Create Different Layouts
To improve user experience on screens of different devices, 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, which uses the screen size-as the suffix. For example, a layout file for large screens should be placed under the res/layout-large/directory.
Note:Android automatically scales your layout to adapt to the screen. In this way, you do not have to consider the absolute size of the UI elements too much for layout files on different screens. Instead, you should focus on the layout structure, this will directly affect the user experience. For example, the size or position of an important view should be relative to the view of the same level ).
For example, the following project contains a default layout and an optional layout directory for large screens:
- MyProject/
- res/
- layout/
- main.xml
- layout-large/
- main.xml
-
The layout file names in the directory must be consistent, but their content can be different to provide an optimized UI to support the corresponding screen size.
As usual, the layout file is referenced in the app:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
The Android system loads layout files from the appropriate layout directory based on the screen size of the device running your app.