How to create an Android app that supports different screen sizes

Source: Internet
Author: User

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:

 
 
  1. MyProject/ 
  2.     res/ 
  3.         layout/ 
  4.             main.xml 
  5.         layout-large/ 
  6.             main.xml 
  7.   

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:

 
 
  1. @Override 
  2.  protected void onCreate(Bundle savedInstanceState) { 
  3.      super.onCreate(savedInstanceState); 
  4.      setContentView(R.layout.main); 
  5.  } 
  6.   

The Android system loads layout files from the appropriate layout directory based on the screen size of the device running your app.

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.