简体   繁体   中英

How to support different screen size and different screen resolutions in android

我正在用android开发应用程序,我必须支持所有不同的屏幕尺寸和密度,例如较低,中等和较高

To develop an application which supports every density and resolution, we should keep following points in mind:

(1) Put different sized images(with the same name) in 4 different folders in the ratio:

ldpi : mdpi : hdpi : xhdpi = 3 : 4 : 6 : 8

(2) The sizes commonly used for these resolutions are:

ldpi = 36 * 36 px mdpi = 48 * 48 px hdpi = 72 * 72 px xhdpi = 96 * 96 px

But you can use your desired sizes for the images as well.(Just try to follow the ratio in different sizes as well.) Amongst of all these images, the image which suits the device's density , will automatically be picked.

(3) Besides this, you can also get the density of the device programmatically and set the layout accordingly, like this:

   DisplayMetrics displayMetrics = new DisplayMetrics();
   getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
   switch(displayMetrics.densityDpi){ 
    case DisplayMetrics.DENSITY_LOW: 
        // layout for small sized devices.
        break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
        // layout for medium-sized devices.
        break; 
    case DisplayMetrics.DENSITY_HIGH: 
        // layout for big-sized devices.
        break; 
   }  

Write Below Code into your Androidmanifest.xml file

<supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM