Android中SP与DP区别的自我理解

从一入Android开发的坑,就被告知这些常识
* 长度宽度的数值要使用dp作为单位放入dimens.xml文件中
* 字体大小的数值要使用sp作为单位放入dimens. xml文件中
然后,就没有然后了,约定俗成的就是这样,到底为什么这样,dp和sp有什么不同?
做个简单的Sample验证一下,一个简单的布局

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! in SP"
        android:textSize="18sp"/>

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World! in DP"
        android:textSize="18dp"/>

得到的效果

然后进入系统设置中,更改字体大小

再次进入之前的界面,就会发现用sp作为字体大小单位,会随着系统的字体大小改变,而dp作为单位则不会

关于sp的API描述为:
Scale-independent Pixels – This is like the dp unit, but it is also scaled by the user’s font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user’s preference.
大致意思是
* sp除了受屏幕密度的影响外,还收到用户字体大小的影响
* 通常情况下,建议使用sp作为字体大小的单位。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值