Android下设置文字大小用sp还是dp

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

结论:

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


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

验证:

main.xml

    <LinearLayout    
        xmlns:toolbar="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"            
        android:layout_height="wrap_content"    
        android:orientation="vertical">    
        <TextView        
            android:layout_width="wrap_content"        
            android:layout_height="wrap_content"        
            android:text="20sp"        
            android:textColor="#FF757233"        
            android:textSize="20sp" />

        <TextView        
            android:layout_width="wrap_content"        
            android:layout_height="wrap_content"        
            android:text="20dp"        
            android:textColor="#FF757233"        
            android:textSize="20dp" />
    </LinearLayout>
修改系统字体大小看看效果
普通字体:
small字体:
large字体:
huge字体:
然而如果你还想知道到底发生了什么事的话就请继续往下看.

在看下面这些内容之前,请你先看看这篇文章:Android下setTextSize的正确使用姿势

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

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. 
 *  
 * <p>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单位时字体大小会受到用户配置系统字体的影响.

附上官方对于sp和dp的讲解

dp:
dp是一种密度无关像素,对应于 160dpi 下像素的物理尺寸
sp:
sp是和dp相同的基本单位,但它会按用户首选的文本尺寸进行缩放(属于缩放无关像素)

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

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

参考:
Android 解决字体随系统调节而变化的问题
设置Android app的字体不随系统全局字体大小的变动而变动
老生常谈之Android里的dp和sp
what is the difference between scaledDensity and density in Android`s DisplayMetrics class?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容