Android的textSize是px,sp,pt,dip单位?

5 阅读1分钟

textSize该用px,sp,pt,dip单位?

<!-- Size of text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp).
         Supported values include the following:<p/>
    <ul>
        <li><b>px</b> Pixels</li>
        <li><b>sp</b> Scaled pixels (scaled to relative pixel size on screen). See {@link android.util.DisplayMetrics} for more information.</li>
        <li><b>pt</b> Points</li>
        <li><b>dip</b> Device independent pixels. See {@link android.util.DisplayMetrics} for more information.</li>
    </ul>
        -->
    <attr name="textSize" format="dimension" />

根据注释说明,推荐使用 sp(scaled-pixels)作为文本大小的单位

详细解释:

各单位的含义:

  • sp(scaled pixels)推荐用于文本

    • 会根据用户的系统字体大小设置进行缩放
    • 确保用户调整系统字体大小时,应用中的文本也能相应调整
    • 提供更好的可访问性
  • dp/dip(density-independent pixels) :用于尺寸(布局、边距等)

    • 根据屏幕密度自动缩放,保持在不同设备上相同的物理尺寸
    • 适合用于控件大小、边距等
  • px(pixels)不推荐

    • 固定像素值,在不同屏幕密度设备上显示大小不一致
    • 可能导致在高密度屏幕上显示过小
  • pt(points) :基于物理尺寸(1/72英寸),但较少使用