常用尺寸单位
- dp
- sp
dp
设备独立像素,会根据屏幕的大小进行改变。主要用于设置边距和组件大小的
sp
可伸缩像素,安卓中推荐的、设置文字大小的单位。他会根据用户字体大小的首选项进行设置文字的大小。例如设置手机的字体是超大。那么同一个程序,在一个屏幕设置文字大小是正常,而另一个设置手机字体是超大的,字体大小是不同的
尺寸资源文件
使用
1、xml文件内使用
<TextView
android:id="@+id/text"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/fab_margin"
android:text="@string/custom_content" />
2、Java文件内使用
text.setTextSize(getResources().getDimension(R.dimen.fab_margin));