Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support

前端 未结 3 521
醉酒成梦
醉酒成梦 2020-11-30 01:18

So we know from many other posts that we should use sp rather than dp for text in Android, and we know the reason for this is to respect a \'user\'s preferences\'.

B

3条回答
  •  日久生厌
    2020-11-30 01:49

    The answer lies in looking at this particular issue holistically.

    The motivation for using "sp" for font sizes lies in giving the developer power to control their layout in the face of user changing the font size on their device.

    Example:

    Lets look at 2 extreme cases:

    1) User selects font size "small"

    This is what my layout looks like:

    http://postimg.org/image/kiyqeo2bh/

    Here is the layout xml:

    
    
    
    
    

    2) If the user selects font size "huge":

    This i what my layout looks like:

    http://postimg.org/image/d7rax9wob/

    My layout xml is same as above in case 1).

    So, as you can see what happened here is the top TextView has sort of perfect font-size in sp because it does not wrap for the entirety of the range of font sizes (small to huge). But the bottom TextView completely messes up your layout/design in case 2).

    So you as a developer can iterate and decide what size in sp works for your design and android will draw it for you.

提交回复
热议问题