Android 理解DPSPPX的区别

Posted 彬sir哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 理解DPSPPX的区别相关的知识,希望对你有一定的参考价值。

基础概念:

dp:最常用的长、宽、margin、padding等的单位
sp:字体的单位,和dp差不多,区别是如果字体使用的sp为单位,那如果你手机字体调大了,那你app的字体会随之变大,如果用dp则不会变化
px:像素

dp、sp、px的Button XML代码如下:

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="150sp"
        android:layout_height="wrap_content"/>

    <Button
        android:layout_width="150px"
        android:layout_height="wrap_content"/>

测试如下:

dp、sp、px的TextView XML代码如下:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Test dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Test sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20px"
        android:text="Test px"/>

测试如下:

以上是关于Android 理解DPSPPX的区别的主要内容,如果未能解决你的问题,请参考以下文章