Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)

前端 未结 3 409
[愿得一人]
[愿得一人] 2020-12-23 15:36

I want to create a XML Layout that will support all the Screen sizes. In the XML, first element is ImageView, second one is TextView and third one

3条回答
  •  清歌不尽
    2020-12-23 16:04

    You need to put all the Widths, Heights, Paddings, Margins , etc in the /res/values/dimens.xml file like this:

    dimens.xml :

    
    157.5dip
    
    
    210dip
    
    
    315dip
    
    
    420dip
    

    And use them in your Layouts (Normal/Medium) like this:

    For converting the dimensions use the following Values:

    0.75 - ldpi  (small)   //mdpi dimens *0.75
    1.0  - mdpi  (normal)  //First create these dimensions
    1.5  - hdpi  (large)   //mdpi dimens *1.5
    2.0  - xhdpi (xLarge)  //mdpi dimens *2.0
    

    You also need to create different Layouts Folders in your res folder for all devices and use the dimensions accordingly.

    Generic Layout Folders (Android Dev Guide) :

    res/layout-small
    res/layout-normal
    res/layout-large
    res/layout-xlarge
    


    After you are done with making your Normal/Medium Layouts follow these steps:

    1. Convert the Normal Dimensions for other Screen Sizes.
    2. Copy your Normal Layout xml files in to other Folders.
    3. Change the suffix of the dimensions used according to the folder that you are in.
    4. Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).

    Then your Layouts should work on every device with correct positioning.
    I hope this helps.

提交回复
热议问题