摩客怎么设置安卓的dp_Android下设置文字大小用sp还是dp

相信很多人看到标题会大吃一惊,长度宽度的数值要用dp,字体的大小用sp,这个理论恐怕在大家心目中早已根深蒂固,官方文档 上也是这样写的

结论:

1、当修改系统字体大小时,字体大小以dp为单位时,大小不变;

2、当修改系统字体大小时,字体大小以sp为单位时,大小跟随变化;

如果你是上面这个表情,那就请继续往下看吧!

验证:

main.xml

xmlns:toolbar="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="20sp"

android:textColor="#FF757233"

android:textSize="20sp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="20dp"

android:textColor="#FF757233"

android:textSize="20dp" />

修改系统字体大小看看效果

普通字体:

small字体:

large字体:

huge字体:

然而如果你还想知道到底发生了什么事的话就请继续往下看.

追溯设置文字大小的源代码,最终会进入到以下方法:

public static float applyDimension(int unit, float value, DisplayMetrics metrics){

switch (unit) {

case COMPLEX_UNIT_PX:

return value;

case COMPLEX_UNIT_DIP:

return value * metrics.density;

case COMPLEX_UNIT_SP:

return value * metrics.scaledDensity;

case COMPLEX_UNIT_PT:

return value * metrics.xdpi * (1.0f/72);

case COMPLEX_UNIT_IN:

return value * metrics.xdpi;

case COMPLEX_UNIT_MM:

return value * metrics.xdpi * (1.0f/25.4f);

}

return 0;

}

在这里我们可以看到sp和dp的区别就是density和scaledDensity的区别

那么这两个有什么区别呢?

/**

* The logical density of the display. This is a scaling factor for the

* Density Independent Pixel unit, where one DIP is one pixel on an

* approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),

* providing the baseline of the system's display. Thus on a 160dpi screen

* this density value will be 1; on a 120 dpi screen it would be .75; etc.

*

*

This value does not exactly follow the real screen size (as given by

* {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of

* the overall UI in steps based on gross changes in the display dpi. For

* example, a 240x320 screen will have a density of 1 even if its width is

* 1.8", 1.3", etc. However, if the screen resolution is increased to

* 320x480 but the screen size remained 1.5"x2" then the density would be * increased (probably to 1.5).

*

* @see #DENSITY_DEFAULT

*/

public float density;

/**

* A scaling factor for fonts displayed on the display. This is the same

* as {@link #density}, except that it may be adjusted in smaller

* increments at runtime based on a user preference for the font size.

*/

public float scaledDensity;

从以上源代码注释可以看到:

scaledDensity会收到用户配置的影响,而density不会.

这也就是为什么设置sp单位时字体大小会受到用户配置系统字体的影响.

dp:

dp是一种密度无关像素,对应于 160dpi 下像素的物理尺寸

sp:

sp是和dp相同的基本单位,但它会按用户首选的文本尺寸进行缩放(属于缩放无关像素)

因此,设置文字大小到底是用sp还是dp,看来还是得根据实际需求来决定呀.

至此,文章结束,希望此文能帮助到你,如果对此文有不同见解,欢迎直接评论!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值