android裏的“陷阱”

前言

人們總是喜歡犯以爲是的錯誤,編程也不例外。於是讓我們看看曾經走過的坑吧

事例:

一、TextSize

易錯點:

我們總以爲:
View.setTextSize(int size1)
int size2=View.getTextSize()

size1和size2單位一樣

實際上API裏是這樣滴:

setTextSize

/**
* Set the default text size to the given value, interpreted as “scaled
* pixel” units. This size is adjusted based on the current density and
* user font size preference.
*
* @param size The scaled pixel size.
*
* @attr ref android.R.styleable#TextView_textSize
*/
@android.view.RemotableViewMethod
public void setTextSize(float size) {
setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
}

getTextSize

/**
* @return the size (in pixels) of the default text size in this TextView.
*/
public float getTextSize() {
return mTextPaint.getTextSize();
}

setTextSize是以sp(scaled pixel)爲單位的,而getTextSize是以px(pixels)爲單位的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章