Android学习笔记尺寸资源

尺寸资源语法

dp:设备独立资源像素

会根据设备匹配大小

一般用于设置边距和组件大小

sp : 可伸缩像素

根据用户手机字体大小首选项进行缩放

使用尺寸资源

定义尺寸资源

dimens

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="title">26sp</dimen>
    <dimen name="padding">30dp</dimen>
</resources>

引用尺寸资源xml法

 <TextView
        android:id="@+id/motto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/title"
        android:padding="@dimen/padding"
        />

引用尺寸资源Java法

 tv_motto.setTextSize(getResources().getDimension(R.dimen.title));
原文地址:https://www.cnblogs.com/lzpq/p/12833574.html