How to define dimens.xml for every different screen size in android?

后端 未结 9 1804
轮回少年
轮回少年 2020-11-22 00:43

When supporting different screen sizes (densities) in Android often the focus is on creating different layouts for every possible screen. I.E.

9条回答
  •  盖世英雄少女心
    2020-11-22 01:04

    Use Scalable DP

    Although making a different layout for different screen sizes is theoretically a good idea, it can get very difficult to accommodate for all screen dimensions, and pixel densities. Having over 20+ different dimens.xml files as suggested in the above answers, is not easy to manage at all.

    How To Use:

    To use sdp:

    1. Include implementation 'com.intuit.sdp:sdp-android:1.0.5' in your build.gradle,
    2. Replace any dp value such as 50dp with a @dimen/50_sdp like so:

      
      

    How It Works:

    sdp scales with the screen size because it is essentially a huge list of different dimens.xml for every possible dp value.

    See It In Action:

    Here it is on three devices with widely differing screen dimensions, and densities:

    Note that the sdp size unit calculation includes some approximation due to some performance and usability constraints.

提交回复
热议问题