做网站现在可以挣钱吗,作文网站投稿,什么自己做网站,什么值得买 网站开发android中#xff0c;对TextView设置文本字体大小#xff0c;是通过在layout xml中设置android:textSize的属性值实现的#xff0c;比如设置“24sp”#xff0c;这里的sp是一种单位#xff0c;其他可选的单位还有px#xff0c;dip(dp)#xff0c;pt#xff0c;in#…android中对TextView设置文本字体大小是通过在layout xml中设置android:textSize的属性值实现的比如设置“24sp”这里的sp是一种单位其他可选的单位还有pxdip(dp)ptinmm建议使用sp。如果要在代码中设置文本字体大小会用到setTextSize传入一个float的值那么这个float值的单位是什么呢我原本以为是px结果不是查看代码发现
public void setTextSize(float size) {setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}
原来默认是sp如果要设置以其他单位的size就要用到带两个参数的setTextSize
public void setTextSize(int unit, float size) {if (!isAutoSizeEnabled()) {setTextSizeInternal(unit, size, true /* shouldRequestLayout */);}
}
第一参数可选的值在TypedValue.java中定义
/** {link #TYPE_DIMENSION} complex unit: Value is raw pixels. */
public static final int COMPLEX_UNIT_PX 0;
/** {link #TYPE_DIMENSION} complex unit: Value is Device Independent* Pixels. */
public static final int COMPLEX_UNIT_DIP 1;
/** {link #TYPE_DIMENSION} complex unit: Value is a scaled pixel. */
public static final int COMPLEX_UNIT_SP 2;
/** {link #TYPE_DIMENSION} complex unit: Value is in points. */
public static final int COMPLEX_UNIT_PT 3;
/** {link #TYPE_DIMENSION} complex unit: Value is in inches. */
public static final int COMPLEX_UNIT_IN 4;
/** {link #TYPE_DIMENSION} complex unit: Value is in millimeters. */
public static final int COMPLEX_UNIT_MM 5;
顺便提一下dp单位和px单位之间的转换但这里用不到。
int dp2px(Context context, float dpValue) {final float scale context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale 0.5f);
}