How to support different screen size in android

前端 未结 9 790
天涯浪人
天涯浪人 2020-11-22 05:29

I\'m developing an app in android and I have to support all different screen sizes and density. So i\'ve created different folder for layout : layout-small layout-larg

相关标签:
9条回答
  • 2020-11-22 05:56

    Use sdp library which is provided in Github

    0 讨论(0)
  • 2020-11-22 05:59

    For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens.

    res/layout/my_layout.xml             // layout for normal screen size ("default")
    res/layout-small/my_layout.xml       // layout for small screen size
    res/layout-large/my_layout.xml       // layout for large screen size
    res/layout-xlarge/my_layout.xml      // layout for extra large screen size
    res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
    
    res/drawable-mdpi/my_icon.png        // bitmap for medium density
    res/drawable-hdpi/my_icon.png        // bitmap for high density
    res/drawable-xhdpi/my_icon.png       // bitmap for extra high density
    

    The following code in the Manifest supports all dpis.

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

    And also check out my SO answer.

    0 讨论(0)
  • 2020-11-22 05:59

    You can use sdp size unit instead of dp size unit. The sdp size unit is relative to the screen size and therefor is often preferred for targeting multiple screen sizes.

    Use it carefully! for example, in most cases you still need to design a different layout for tablets.

    0 讨论(0)
  • 2020-11-22 06:04

    Beginning with Android 3.2 (API level 13), size groups (folders small, normal, large, xlarge) are deprecated in favor of a new technique for managing screen sizes based on the available screen width.



    There are different resource configurations that you can specify based on the space available for your layout:

    1) Smallest Width - The fundamental size of a screen, as indicated by the shortest dimension of the available screen area.

    Qualifier Value: sw'dp value'dp

    Eg. res/sw600dp/layout.xml -> will be used for all screen sizes bigger or equal to 600dp. This does not take the device orientation into account.


    2) Available Screen Width - Specifies a minimum available width in dp units at which the resources should be used.

    Qualifier Value: w'dp value'dp

    Eg. res/w600dp/layout.xml -> will be used for all screens, which width is greater than or equal to 600dp.


    3) Available Screen Height - Specifies a minimum screen height in dp units at which the resources should be used.

    Qualifier Value: h'dp value'dp

    Eg. res/h600dp/layout.xml -> will be used for all screens, which height is greater than or equal to 600dp.



    So at the end your folder structure might look like this:

    res/layout/layout.xml -> for handsets (smaller than 600dp available width)
    res/layout-sw600dp/layout.xml -> for 7” tablets (600dp wide and bigger)
    res/layout-sw720dp/layout.xml -> for 10” tablets (720dp wide and bigger)


    For more information please read the official documentation:
    https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

    0 讨论(0)
  • 2020-11-22 06:08

    It sounds lofty,when it comes to supporting multiple screen Sizes.The following gves better results .

    res/layout/layout-w120dp
    res/layout/layout-w160dp
    res/layout/layout-w240dp
    res/layout/layout-w160dp
    res/layout/layout-w320dp
    res/layout/layout-w480dp
    res/layout/layout-w600dp
    res/layout/layout-w720dp
    

    Chek the Device Width and Height using Display Metrics

    Place/figure out which layout suits for the resulted width of the Device .

    let smallestScreenWidthDp="assume some value(Which will be derived from Display metrics)"
    

    All should be checked before setContentView().Otherwise you put yourself in trouble

         Configuration config = getResources().getConfiguration();
    
         Configuration config = getResources().getConfiguration();
    
            if (config.smallestScreenWidthDp >= 600) {
                setContentView(R.layout.layout-w600dp);
            } else {
                setContentView(R.layout.main_activity);
            }
    

    In the top,i have created so many layouts to fit multiple screens,it is all depends on you ,you may or not.You can see the play store reviews from Which API ,The Downloads are High..form that you have to proceed.

    I hope it helps you lot.Few were using some third party libraries,It may be reduce your work ,but that is not best practice. Get Used to Android Best Practices.

    Check This Out

    0 讨论(0)
  • 2020-11-22 06:09

    Android adjust by it self you can put separate image for different folder if you want to use different images for high resolution devices and other device. Otherwise just put in one drawable,layout folder only for some images you can make 9-patch also.

    read here

    you need permission in manifest for multiple screen support link

    <supports-screens android:resizeable=["true"| "false"]
                      android:smallScreens=["true" | "false"]
                      android:normalScreens=["true" | "false"]
                      android:largeScreens=["true" | "false"]
                      android:xlargeScreens=["true" | "false"]
                      android:anyDensity=["true" | "false"]
                      android:requiresSmallestWidthDp="integer"
                      android:compatibleWidthLimitDp="integer"
                      android:largestWidthLimitDp="integer"/>
    
    0 讨论(0)
提交回复
热议问题